diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index bc14d4c5392d..5e8730c71e58 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -545,6 +545,8 @@ struct AdditionalKeywords { kw_throws = &IdentTable.get("throws"); kw___except = &IdentTable.get("__except"); + kw_mark = &IdentTable.get("mark"); + kw_option = &IdentTable.get("option"); kw_optional = &IdentTable.get("optional"); kw_repeated = &IdentTable.get("repeated"); @@ -582,6 +584,9 @@ struct AdditionalKeywords { IdentifierInfo *kw_synchronized; IdentifierInfo *kw_throws; + // Pragma keywords. + IdentifierInfo *kw_mark; + // Proto keywords. IdentifierInfo *kw_option; IdentifierInfo *kw_optional; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index ea5503ade68d..7733b8a6e951 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -582,11 +582,14 @@ private: void parsePragma() { next(); // Consume "pragma". - if (CurrentToken && CurrentToken->TokenText == "mark") { + if (CurrentToken && + CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) { + bool IsMark = CurrentToken->is(Keywords.kw_mark); next(); // Consume "mark". next(); // Consume first token (so we fix leading whitespace). while (CurrentToken) { - CurrentToken->Type = TT_ImplicitStringLiteral; + if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator)) + CurrentToken->Type = TT_ImplicitStringLiteral; next(); } } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index bec7a9cfb50a..ed31a097b0ab 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8795,6 +8795,12 @@ TEST_F(FormatTest, UnderstandsPragmas) { "(including parentheses).")); } +TEST_F(FormatTest, UnderstandPragmaOption) { + verifyFormat("#pragma option -C -A"); + + EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); +} + #define EXPECT_ALL_STYLES_EQUAL(Styles) \ for (size_t i = 1; i < Styles.size(); ++i) \ EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \