From: Daniel Jasper Date: Fri, 23 Jan 2015 19:37:25 +0000 (+0000) Subject: clang-format: Fix another crasher caused by incomplete macro code. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dbd59c516ce77b47a8aff1a24c3939e63fe99110;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@226930 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp index ca66e73516..6c4aadc0f1 100644 --- a/lib/Format/UnwrappedLineFormatter.cpp +++ b/lib/Format/UnwrappedLineFormatter.cpp @@ -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 &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; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index faf7c43688..65a7bff599 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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) {