clang-format: Fix another crasher caused by incomplete macro code.

We did't properly mark all of an AnnotatedLine's children as finalized
and thus would reformat the same tokens in different branches of #if/#else
sequences leading to invalid replacements.

llvm-svn: 226930
This commit is contained in:
Daniel Jasper 2015-01-23 19:37:25 +00:00
parent 9a36b3e147
commit d1c13736e0
2 changed files with 12 additions and 5 deletions

View File

@ -309,6 +309,15 @@ private:
ContinuationIndenter *Indenter;
};
static void markFinalized(FormatToken *Tok) {
for (; Tok; Tok = Tok->Next) {
Tok->Finalized = true;
for (AnnotatedLine *Child : Tok->Children)
markFinalized(Child->First);
}
}
} // namespace
unsigned
@ -442,11 +451,8 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
}
}
}
if (!DryRun) {
for (FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) {
Tok->Finalized = true;
}
}
if (!DryRun)
markFinalized(TheLine.First);
PreviousLine = *I;
}
PenaltyCache[CacheKey] = Penalty;

View File

@ -2614,6 +2614,7 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
}
TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {