r235492 - clang-format: Fix for #pragma option formatting.

Daniel Jasper djasper at google.com
Wed Apr 22 02:45:43 PDT 2015


Author: djasper
Date: Wed Apr 22 04:45:42 2015
New Revision: 235492

URL: http://llvm.org/viewvc/llvm-project?rev=235492&view=rev
Log:
clang-format: Fix for #pragma option formatting.

Adapted patch from Sergey Razmetov. Thank you.

Modified:
    cfe/trunk/lib/Format/FormatToken.h
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=235492&r1=235491&r2=235492&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Wed Apr 22 04:45:42 2015
@@ -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;

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=235492&r1=235491&r2=235492&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Apr 22 04:45:42 2015
@@ -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();
       }
     }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=235492&r1=235491&r2=235492&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Apr 22 04:45:42 2015
@@ -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 "                \





More information about the cfe-commits mailing list