From 948ee9b211f06b03795f459d332330742ea5d4c5 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 1 Mar 2017 10:47:52 +0000 Subject: [PATCH] clang-format: Ignore contents of #ifdef SWIG .. #endif blocks. 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 | 12 ++++++------ unittests/Format/FormatTestComments.cpp | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 9a536ad601..bd049ef668 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -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(); } diff --git a/unittests/Format/FormatTestComments.cpp b/unittests/Format/FormatTestComments.cpp index 049851793c..cb1cefa47c 100644 --- a/unittests/Format/FormatTestComments.cpp +++ b/unittests/Format/FormatTestComments.cpp @@ -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" -- 2.40.0