From: Daniel Jasper Date: Wed, 22 Apr 2015 09:45:42 +0000 (+0000) Subject: clang-format: Fix for #pragma option formatting. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fda111fa4064ad249e9a9dfc8989c5d7f85179b6;p=clang clang-format: Fix for #pragma option formatting. Adapted patch from Sergey Razmetov. Thank you. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@235492 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index bc14d4c539..5e8730c71e 100644 --- a/lib/Format/FormatToken.h +++ b/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/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index ea5503ade6..7733b8a6e9 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index bec7a9cfb5..ed31a097b0 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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 " \