owenpan created this revision. owenpan added reviewers: MyDeveloperDay, HazardyKnusperkeks, rymiel. owenpan added a project: clang-format. Herald added a project: All. owenpan requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Closes https://github.com/llvm/llvm-project/issues/60264. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D142804 Files: clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/FormatTokenLexer.cpp clang/lib/Format/IntegerLiteralSeparatorFixer.cpp clang/lib/Format/SortJavaScriptImports.cpp clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -22619,6 +22619,28 @@ "/* clang-format off */\n" "/* long long long long long long line */\n", getLLVMStyleWithColumns(20))); + + verifyFormat("int *i;\n" + "// clang-format off 0\n" + "int* j;\n" + "// clang-format on 1\n" + "int *k;", + "int* i;\n" + "// clang-format off 0\n" + "int* j;\n" + "// clang-format on 1\n" + "int* k;"); + + verifyFormat("int *i;\n" + "// clang-format off\t0\n" + "int* j;\n" + "// clang-format on1\n" + "int* k;", + "int* i;\n" + "// clang-format off\t0\n" + "int* j;\n" + "// clang-format on1\n" + "int* k;"); } TEST_F(FormatTest, DoNotCrashOnInvalidInput) { Index: clang/lib/Format/SortJavaScriptImports.cpp =================================================================== --- clang/lib/Format/SortJavaScriptImports.cpp +++ clang/lib/Format/SortJavaScriptImports.cpp @@ -195,8 +195,7 @@ // Separate references from the main code body of the file. if (FirstNonImportLine && FirstNonImportLine->First->NewlinesBefore < 2 && !(FirstNonImportLine->First->is(tok::comment) && - FirstNonImportLine->First->TokenText.trim() == - "// clang-format on")) { + isClangFormatOn(FirstNonImportLine->First->TokenText.trim()))) { ReferencesText += "\n"; } @@ -376,9 +375,9 @@ // This is tracked in FormattingOff here and on JsModuleReference. while (Current && Current->is(tok::comment)) { StringRef CommentText = Current->TokenText.trim(); - if (CommentText == "// clang-format off") { + if (isClangFormatOff(CommentText)) { FormattingOff = true; - } else if (CommentText == "// clang-format on") { + } else if (isClangFormatOn(CommentText)) { FormattingOff = false; // Special case: consider a trailing "clang-format on" line to be part // of the module reference, so that it gets moved around together with Index: clang/lib/Format/IntegerLiteralSeparatorFixer.cpp =================================================================== --- clang/lib/Format/IntegerLiteralSeparatorFixer.cpp +++ clang/lib/Format/IntegerLiteralSeparatorFixer.cpp @@ -87,9 +87,9 @@ auto Location = Tok.getLocation(); auto Text = StringRef(SourceMgr.getCharacterData(Location), Length); if (Tok.is(tok::comment)) { - if (Text == "// clang-format off" || Text == "/* clang-format off */") + if (isClangFormatOff(Text)) Skip = true; - else if (Text == "// clang-format on" || Text == "/* clang-format on */") + else if (isClangFormatOn(Text)) Skip = false; continue; } Index: clang/lib/Format/FormatTokenLexer.cpp =================================================================== --- clang/lib/Format/FormatTokenLexer.cpp +++ clang/lib/Format/FormatTokenLexer.cpp @@ -1286,17 +1286,13 @@ Tok.Tok.setKind(tok::string_literal); } - if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" || - Tok.TokenText == "/* clang-format on */")) { + if (Tok.is(tok::comment) && isClangFormatOn(Tok.TokenText)) FormattingDisabled = false; - } Tok.Finalized = FormattingDisabled; - if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" || - Tok.TokenText == "/* clang-format off */")) { + if (Tok.is(tok::comment) && isClangFormatOff(Tok.TokenText)) FormattingDisabled = true; - } } void FormatTokenLexer::resetLexer(unsigned Offset) { Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -3002,13 +3002,10 @@ if (Trimmed.contains(RawStringTermination)) FormattingOff = false; - if (Trimmed == "// clang-format off" || - Trimmed == "/* clang-format off */") { + if (isClangFormatOff(Trimmed)) FormattingOff = true; - } else if (Trimmed == "// clang-format on" || - Trimmed == "/* clang-format on */") { + else if (isClangFormatOn(Trimmed)) FormattingOff = false; - } const bool EmptyLineSkipped = Trimmed.empty() && @@ -3185,9 +3182,9 @@ Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev); StringRef Trimmed = Line.trim(); - if (Trimmed == "// clang-format off") + if (isClangFormatOff(Trimmed)) FormattingOff = true; - else if (Trimmed == "// clang-format on") + else if (isClangFormatOn(Trimmed)) FormattingOff = false; if (ImportRegex.match(Line, &Matches)) { @@ -3893,5 +3890,27 @@ return FallbackStyle; } +bool isClangFormatOn(StringRef Comment) { + if (Comment == "/* clang-format on */") + return true; + + static const StringRef Prefix("// clang-format on"); + static const auto Size = Prefix.size(); + + return Comment.startswith(Prefix) && + (Comment.size() == Size || isblank(Comment[Size])); +} + +bool isClangFormatOff(StringRef Comment) { + if (Comment == "/* clang-format off */") + return true; + + static const StringRef Prefix("// clang-format off"); + static const auto Size = Prefix.size(); + + return Comment.startswith(Prefix) && + (Comment.size() == Size || isblank(Comment[Size])); +} + } // namespace format } // namespace clang Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -4599,6 +4599,9 @@ } } +bool isClangFormatOn(StringRef Comment); +bool isClangFormatOff(StringRef Comment); + } // end namespace format } // end namespace clang
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits