@@ -3267,9 +3267,9 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
3267
3267
3268
3268
bool IsBlockComment = false ;
3269
3269
3270
- if (isClangFormatOff (Trimmed)) {
3270
+ if (parseClangFormatDirective (Trimmed) == ClangFormatDirective::Off ) {
3271
3271
FormattingOff = true ;
3272
- } else if (isClangFormatOn (Trimmed)) {
3272
+ } else if (parseClangFormatDirective (Trimmed) == ClangFormatDirective::On ) {
3273
3273
FormattingOff = false ;
3274
3274
} else if (Trimmed.starts_with (" /*" )) {
3275
3275
IsBlockComment = true ;
@@ -3452,9 +3452,9 @@ tooling::Replacements sortJavaImports(const FormatStyle &Style, StringRef Code,
3452
3452
Code.substr (Prev, (Pos != StringRef::npos ? Pos : Code.size ()) - Prev);
3453
3453
3454
3454
StringRef Trimmed = Line.trim ();
3455
- if (isClangFormatOff (Trimmed))
3455
+ if (parseClangFormatDirective (Trimmed) == ClangFormatDirective::Off )
3456
3456
FormattingOff = true ;
3457
- else if (isClangFormatOn (Trimmed))
3457
+ else if (parseClangFormatDirective (Trimmed) == ClangFormatDirective::On )
3458
3458
FormattingOff = false ;
3459
3459
3460
3460
if (ImportRegex.match (Line, &Matches)) {
@@ -4190,24 +4190,45 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
4190
4190
return FallbackStyle;
4191
4191
}
4192
4192
4193
- static bool isClangFormatOnOff (StringRef Comment, bool On) {
4194
- if (Comment == (On ? " /* clang-format on */" : " /* clang-format off */" ))
4195
- return true ;
4193
+ static unsigned skipWhitespace (unsigned Pos, StringRef Str, unsigned Length) {
4194
+ while (Pos < Length && isspace (Str[Pos]))
4195
+ ++Pos;
4196
+ return Pos;
4197
+ }
4196
4198
4197
- static const char ClangFormatOn[] = " // clang-format on" ;
4198
- static const char ClangFormatOff[] = " // clang-format off" ;
4199
- const unsigned Size = (On ? sizeof ClangFormatOn : sizeof ClangFormatOff) - 1 ;
4199
+ ClangFormatDirective parseClangFormatDirective (StringRef Comment) {
4200
+ size_t Pos = std::min (Comment.find (" /*" ), Comment.find (" //" ));
4201
+ unsigned Length = Comment.size ();
4202
+ if (Pos == StringRef::npos)
4203
+ return ClangFormatDirective::None;
4200
4204
4201
- return Comment.starts_with (On ? ClangFormatOn : ClangFormatOff) &&
4202
- (Comment.size () == Size || Comment[Size] == ' :' );
4203
- }
4205
+ Pos = skipWhitespace (Pos + 2 , Comment, Length);
4206
+ StringRef ClangFormatDirectiveName = " clang-format" ;
4204
4207
4205
- bool isClangFormatOn (StringRef Comment) {
4206
- return isClangFormatOnOff (Comment, /* On=*/ true );
4207
- }
4208
+ if (Comment.substr (Pos, ClangFormatDirectiveName.size ()) ==
4209
+ ClangFormatDirectiveName) {
4210
+ Pos =
4211
+ skipWhitespace (Pos + ClangFormatDirectiveName.size (), Comment, Length);
4212
+
4213
+ unsigned EndDirectiveValuePos = Pos;
4214
+ while (EndDirectiveValuePos < Length) {
4215
+ char Char = Comment[EndDirectiveValuePos];
4216
+ if (isspace (Char) || Char == ' *' || Char == ' :' )
4217
+ break ;
4218
+
4219
+ ++EndDirectiveValuePos;
4220
+ }
4221
+
4222
+ return llvm::StringSwitch<ClangFormatDirective>(
4223
+ Comment.substr (Pos, EndDirectiveValuePos - Pos))
4224
+ .Case (" off" , ClangFormatDirective::Off)
4225
+ .Case (" on" , ClangFormatDirective::On)
4226
+ .Case (" off-line" , ClangFormatDirective::OffLine)
4227
+ .Case (" off-next-line" , ClangFormatDirective::OffNextLine)
4228
+ .Default (ClangFormatDirective::None);
4229
+ }
4208
4230
4209
- bool isClangFormatOff (StringRef Comment) {
4210
- return isClangFormatOnOff (Comment, /* On=*/ false );
4231
+ return ClangFormatDirective::None;
4211
4232
}
4212
4233
4213
4234
} // namespace format
0 commit comments