This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rGd5be1550f140: [clang-format] Don't crash on malformed preprocessor conditions (authored by sstwcw).
Changed prior to commit: https://reviews.llvm.org/D135972?vs=470623&id=471804#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D135972/new/ https://reviews.llvm.org/D135972 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -5193,6 +5193,34 @@ 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)}"); + verifyNoCrash("#else\n" + "#else\n" + "#endif\n" + "#endif"); + verifyNoCrash("#else\n" + "#if X\n" + "#endif\n" + "#endif"); + verifyNoCrash("#else\n" + "#endif\n" + "#if X\n" + "#endif"); + verifyNoCrash("#if X\n" + "#else\n" + "#else\n" + "#endif\n" + "#endif"); + verifyNoCrash("#if X\n" + "#elif Y\n" + "#elif Y\n" + "#endif\n" + "#endif"); + verifyNoCrash("#endif\n" + "#endif"); + verifyNoCrash("#endif\n" + "#else"); + verifyNoCrash("#endif\n" + "#elif Y"); } TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1215,9 +1215,12 @@ // If a potential include guard has an #else, it's not an include guard. if (IncludeGuard == IG_Defined && PPBranchLevel == 0) IncludeGuard = IG_Rejected; + // Don't crash when there is an #else without an #if. + assert(PPBranchLevel >= -1); + if (PPBranchLevel == -1) + conditionalCompilationStart(/*Unreachable=*/true); conditionalCompilationAlternative(); - if (PPBranchLevel > -1) - --PPBranchLevel; + --PPBranchLevel; parsePPUnknown(); ++PPBranchLevel; }
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -5193,6 +5193,34 @@ 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)}"); + verifyNoCrash("#else\n" + "#else\n" + "#endif\n" + "#endif"); + verifyNoCrash("#else\n" + "#if X\n" + "#endif\n" + "#endif"); + verifyNoCrash("#else\n" + "#endif\n" + "#if X\n" + "#endif"); + verifyNoCrash("#if X\n" + "#else\n" + "#else\n" + "#endif\n" + "#endif"); + verifyNoCrash("#if X\n" + "#elif Y\n" + "#elif Y\n" + "#endif\n" + "#endif"); + verifyNoCrash("#endif\n" + "#endif"); + verifyNoCrash("#endif\n" + "#else"); + verifyNoCrash("#endif\n" + "#elif Y"); } TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1215,9 +1215,12 @@ // If a potential include guard has an #else, it's not an include guard. if (IncludeGuard == IG_Defined && PPBranchLevel == 0) IncludeGuard = IG_Rejected; + // Don't crash when there is an #else without an #if. + assert(PPBranchLevel >= -1); + if (PPBranchLevel == -1) + conditionalCompilationStart(/*Unreachable=*/true); conditionalCompilationAlternative(); - if (PPBranchLevel > -1) - --PPBranchLevel; + --PPBranchLevel; parsePPUnknown(); ++PPBranchLevel; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits