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.

llvm-svn: 296605
This commit is contained in:
Daniel Jasper 2017-03-01 10:47:52 +00:00
parent 413671507f
commit eab6cd474c
2 changed files with 14 additions and 6 deletions

View File

@ -590,12 +590,12 @@ void UnwrappedLineParser::conditionalCompilationEnd() {
void UnwrappedLineParser::parsePPIf(bool IfDef) { void UnwrappedLineParser::parsePPIf(bool IfDef) {
nextToken(); nextToken();
bool IsLiteralFalse = (FormatTok->Tok.isLiteral() && bool Unreachable = false;
FormatTok->Tok.getLiteralData() != nullptr && if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
StringRef(FormatTok->Tok.getLiteralData(), Unreachable = true;
FormatTok->Tok.getLength()) == "0") || if (IfDef && FormatTok->TokenText == "SWIG")
FormatTok->Tok.is(tok::kw_false); Unreachable = true;
conditionalCompilationStart(!IfDef && IsLiteralFalse); conditionalCompilationStart(Unreachable);
parsePPUnknown(); parsePPUnknown();
} }

View File

@ -1683,6 +1683,14 @@ TEST_F(FormatTestComments, IgnoresIf0Contents) {
"void f( ) { }\n" "void f( ) { }\n"
"#endif\n" "#endif\n"
"void g( ) { }\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" EXPECT_EQ("enum E {\n"
" One,\n" " One,\n"
" Two,\n" " Two,\n"