@@ -3267,9 +3267,9 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
32673267
32683268 bool IsBlockComment = false ;
32693269
3270- if (isClangFormatOff (Trimmed)) {
3270+ if (parseClangFormatDirective (Trimmed) == ClangFormatDirective::Off ) {
32713271 FormattingOff = true ;
3272- } else if (isClangFormatOn (Trimmed)) {
3272+ } else if (parseClangFormatDirective (Trimmed) == ClangFormatDirective::On ) {
32733273 FormattingOff = false ;
32743274 } else if (Trimmed.starts_with (" /*" )) {
32753275 IsBlockComment = true ;
@@ -3452,9 +3452,9 @@ tooling::Replacements sortJavaImports(const FormatStyle &Style, StringRef Code,
34523452 Code.substr (Prev, (Pos != StringRef::npos ? Pos : Code.size ()) - Prev);
34533453
34543454 StringRef Trimmed = Line.trim ();
3455- if (isClangFormatOff (Trimmed))
3455+ if (parseClangFormatDirective (Trimmed) == ClangFormatDirective::Off )
34563456 FormattingOff = true ;
3457- else if (isClangFormatOn (Trimmed))
3457+ else if (parseClangFormatDirective (Trimmed) == ClangFormatDirective::On )
34583458 FormattingOff = false ;
34593459
34603460 if (ImportRegex.match (Line, &Matches)) {
@@ -4190,24 +4190,45 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
41904190 return FallbackStyle;
41914191}
41924192
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+ }
41964198
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;
42004204
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" ;
42044207
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 == ' *' )
4217+ break ;
4218+
4219+ ++EndDirectiveValuePos;
4220+ }
4221+
4222+ return llvm::StringSwitch<ClangFormatDirective>(
4223+ Comment.substr (Pos, EndDirectiveValuePos - Pos))
4224+ .Cases (" off" , " off:" , ClangFormatDirective::Off)
4225+ .Cases (" on" , " on:" , ClangFormatDirective::On)
4226+ .Case (" off-line" , ClangFormatDirective::OffLine)
4227+ .Case (" off-next-line" , ClangFormatDirective::OffNextLine)
4228+ .Default (ClangFormatDirective::None);
4229+ }
42084230
4209- bool isClangFormatOff (StringRef Comment) {
4210- return isClangFormatOnOff (Comment, /* On=*/ false );
4231+ return ClangFormatDirective::None;
42114232}
42124233
42134234} // namespace format
0 commit comments