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) {
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();
}

View 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"