]> granicus.if.org Git - clang/commitdiff
clang-format: Ignore contents of #ifdef SWIG .. #endif blocks.
authorDaniel Jasper <djasper@google.com>
Wed, 1 Mar 2017 10:47:52 +0000 (10:47 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 1 Mar 2017 10:47:52 +0000 (10:47 +0000)
Those blocks are used if C++ code is SWIG-wrapped (see swig.org) and
usually do not contain C++ code. Also cleanup the implementation of for #if 0
and #if false a bit.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@296605 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestComments.cpp

index 9a536ad60193b784216469bd2e62713637e3f15b..bd049ef668b2ed6be797d9230133f4f4e78bf625 100644 (file)
@@ -590,12 +590,12 @@ void UnwrappedLineParser::conditionalCompilationEnd() {
 
 void UnwrappedLineParser::parsePPIf(bool IfDef) {
   nextToken();
-  bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
-                         FormatTok->Tok.getLiteralData() != nullptr &&
-                         StringRef(FormatTok->Tok.getLiteralData(),
-                                   FormatTok->Tok.getLength()) == "0") ||
-                        FormatTok->Tok.is(tok::kw_false);
-  conditionalCompilationStart(!IfDef && IsLiteralFalse);
+  bool Unreachable = false;
+  if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
+    Unreachable = true;
+  if (IfDef && FormatTok->TokenText == "SWIG")
+    Unreachable = true;
+  conditionalCompilationStart(Unreachable);
   parsePPUnknown();
 }
 
index 049851793c0c45fd5d6e9a2309a1d88ca40875fb..cb1cefa47cf63b0e248b73a9df4ae368bfbb8d8c 100644 (file)
@@ -1683,6 +1683,14 @@ TEST_F(FormatTestComments, IgnoresIf0Contents) {
                    "void f(  ) {  }\n"
                    "#endif\n"
                    "void g(  ) {  }\n"));
+  EXPECT_EQ("#ifdef SWIG\n"
+            "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
+            "#endif\n"
+            "void f() {}",
+            format("#ifdef SWIG\n"
+                   "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
+                   "#endif\n"
+                   "void f(  ) {  }"));
   EXPECT_EQ("enum E {\n"
             "  One,\n"
             "  Two,\n"