From: Jake Merdich Date: Fri, 26 Jun 2020 01:59:33 +0000 (-0400) Subject: [clang-format] Preserve whitespace in selected macros X-Git-Tag: llvmorg-12-init~1616 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0c332a7784c649038bd237a60fa18b45a3dea90d;p=platform%2Fupstream%2Fllvm.git [clang-format] Preserve whitespace in selected macros Summary: https://bugs.llvm.org/show_bug.cgi?id=46383 When the c preprocessor stringizes tokens, the generated string literals are affected by the whitespace. This means clang-format can affect codegen silently, adding spaces and newlines to strings. Practically speaking, the vast majority of cases will be harmless, only affecting single identifiers or debug macros. In the interest of doing no harm in other cases though, this introduces a blacklist option 'WhitespaceSensitiveMacros', which contains a list of names of function-like macros whose contents should not be touched by clang-format, period. Clang-format can't automatically detect these without a real compile context, so users will have to specify it explicitly (it still beats clang-format off'ing at every invocation). Defaults include "STRINGIZE", "PP_STRINGIZE", and "BOOST_PP_STRINGIZE". Subscribers: kristof.beyls, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D82620 --- diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 496e4c6..e846767 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -2694,6 +2694,23 @@ the configuration (without a prefix: ``Auto``). Use tabs whenever we need to fill whitespace that spans at least from one tab stop to the next one. +**WhitespaceSensitiveMacros** (``std::vector``) + A vector of macros which are whitespace-sensitive and should not be touched. + + These are expected to be macros of the form: + + .. code-block:: c++ + + STRINGIZE(...) + + In the .clang-format configuration file, this can be configured like: + + .. code-block:: yaml + + WhitespaceSensitiveMacros: ['STRINGIZE', 'PP_STRINGIZE'] + + For example: BOOST_PP_STRINGIZE. + .. END_FORMAT_STYLE_OPTIONS diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 96b7321..3549ec9 100755 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1425,6 +1425,17 @@ struct FormatStyle { /// For example: TESTSUITE std::vector NamespaceMacros; + /// A vector of macros which are whitespace-sensitive and shouldn't be + /// touched. + /// + /// These are expected to be macros of the form: + /// \code + /// STRINGIZE(...) + /// \endcode + /// + /// For example: STRINGIZE + std::vector WhitespaceSensitiveMacros; + tooling::IncludeStyle IncludeStyle; /// Indent case labels one level from the switch statement. diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index d83410b..0d277a6 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -599,6 +599,8 @@ template <> struct MappingTraits { IO.mapOptional("TypenameMacros", Style.TypenameMacros); IO.mapOptional("UseCRLF", Style.UseCRLF); IO.mapOptional("UseTab", Style.UseTab); + IO.mapOptional("WhitespaceSensitiveMacros", + Style.WhitespaceSensitiveMacros); } }; @@ -933,6 +935,9 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.SortUsingDeclarations = true; LLVMStyle.StatementMacros.push_back("Q_UNUSED"); LLVMStyle.StatementMacros.push_back("QT_REQUIRE_VERSION"); + LLVMStyle.WhitespaceSensitiveMacros.push_back("STRINGIZE"); + LLVMStyle.WhitespaceSensitiveMacros.push_back("PP_STRINGIZE"); + LLVMStyle.WhitespaceSensitiveMacros.push_back("BOOST_PP_STRINGIZE"); // Defaults that differ when not C++. if (Language == FormatStyle::LK_TableGen) { diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 2ad839a..d4287f5 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -102,6 +102,7 @@ namespace format { TYPE(TrailingUnaryOperator) \ TYPE(TypenameMacro) \ TYPE(UnaryOperator) \ + TYPE(UntouchableMacroFunc) \ TYPE(CSharpStringLiteral) \ TYPE(CSharpNamedArgumentColon) \ TYPE(CSharpNullable) \ diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index f64ab38..dbbe6f8 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -43,6 +43,11 @@ FormatTokenLexer::FormatTokenLexer(const SourceManager &SourceMgr, FileID ID, Macros.insert({&IdentTable.get(TypenameMacro), TT_TypenameMacro}); for (const std::string &NamespaceMacro : Style.NamespaceMacros) Macros.insert({&IdentTable.get(NamespaceMacro), TT_NamespaceMacro}); + for (const std::string &WhitespaceSensitiveMacro : + Style.WhitespaceSensitiveMacros) { + Macros.insert( + {&IdentTable.get(WhitespaceSensitiveMacro), TT_UntouchableMacroFunc}); + } } ArrayRef FormatTokenLexer::lex() { diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6e8dc69..a74015d 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -160,6 +160,27 @@ private: return false; } + bool parseUntouchableParens() { + while (CurrentToken) { + CurrentToken->Finalized = true; + switch (CurrentToken->Tok.getKind()) { + case tok::l_paren: + next(); + if (!parseUntouchableParens()) + return false; + continue; + case tok::r_paren: + next(); + return true; + default: + // no-op + break; + } + next(); + } + return false; + } + bool parseParens(bool LookForDecls = false) { if (!CurrentToken) return false; @@ -171,6 +192,11 @@ private: Contexts.back().ColonIsForRangeExpr = Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr; + if (Left->Previous && Left->Previous->is(TT_UntouchableMacroFunc)) { + Left->Finalized = true; + return parseUntouchableParens(); + } + bool StartsObjCMethodExpr = false; if (FormatToken *MaybeSel = Left->Previous) { // @selector( starts a selector. @@ -1311,7 +1337,7 @@ private: TT_TypenameMacro, TT_FunctionLBrace, TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator, TT_RegexLiteral, TT_TemplateString, - TT_ObjCStringLiteral)) + TT_ObjCStringLiteral, TT_UntouchableMacroFunc)) CurrentToken->setType(TT_Unknown); CurrentToken->Role.reset(); CurrentToken->MatchingParen = nullptr; @@ -3970,7 +3996,7 @@ void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { << " C=" << Tok->CanBreakBefore << " T=" << getTokenTypeName(Tok->getType()) << " S=" << Tok->SpacesRequiredBefore - << " B=" << Tok->BlockParameterCount + << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount << " BK=" << Tok->BlockKind << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind << " FakeLParens="; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index a47c66c..ff9a64e 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -14021,6 +14021,19 @@ TEST_F(FormatTest, ParsesConfiguration) { CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, std::vector({"TESTSUITE", "SUITE"})); + Style.WhitespaceSensitiveMacros.clear(); + CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", + WhitespaceSensitiveMacros, std::vector{"STRINGIZE"}); + CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", + WhitespaceSensitiveMacros, + std::vector({"STRINGIZE", "ASSERT"})); + Style.WhitespaceSensitiveMacros.clear(); + CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", + WhitespaceSensitiveMacros, std::vector{"STRINGIZE"}); + CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", + WhitespaceSensitiveMacros, + std::vector({"STRINGIZE", "ASSERT"})); + Style.IncludeStyle.IncludeCategories.clear(); std::vector ExpectedCategories = { {"abc/.*", 2, 0}, {".*", 1, 0}}; @@ -16530,6 +16543,36 @@ TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { verifyFormat("foo(operator, , -42);", Style); } +TEST_F(FormatTest, WhitespaceSensitiveMacros) { + FormatStyle Style = getLLVMStyle(); + Style.WhitespaceSensitiveMacros.push_back("FOO"); + + // Don't use the helpers here, since 'mess up' will change the whitespace + // and these are all whitespace sensitive by definition + EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", + format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); + EXPECT_EQ( + "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", + format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); + EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", + format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); + EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" + " Still=Intentional);", + format("FOO(String-ized&Messy+But,: :\n" + " Still=Intentional);", + Style)); + Style.AlignConsecutiveAssignments = true; + EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" + " Still=Intentional);", + format("FOO(String-ized=&Messy+But,: :\n" + " Still=Intentional);", + Style)); + + Style.ColumnLimit = 21; + EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", + format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); +} + TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { // These tests are not in NamespaceFixer because that doesn't // test its interaction with line wrapping