[PATCH] D135972: [clang-format] Don't crash on malformed preprocessor conditions

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 21 00:16:06 PDT 2022


owenpan added inline comments.


================
Comment at: clang/lib/Format/UnwrappedLineParser.cpp:1216
+  // Don't crash when there is an #else without an #if.
+  if (PPBranchLevel <= -1) {
+    conditionalCompilationStart(/*Unreachable=*/true);
----------------
Nit.


================
Comment at: clang/unittests/Format/FormatTest.cpp:5196-5206
+  std::function<void(std::string, unsigned)> FormatBadBranches =
+      [&](std::string Prefix, unsigned Lines) {
+        const std::string Directives[] = {"", "#if X\n", "#else X\n",
+                                          "#endif\n"};
+        if (Lines == 0)
+          verifyNoCrash(Prefix);
+        else
----------------
Can we have individual `verifyFormat` or `verifyNoCrash`tests for easy reading and to avoid the overhead? Some examples:
```
#else
a;
#if X
b;
#endif
#endif

#elif X
a;
#endif
#ifdef X
b;
#endif
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135972/new/

https://reviews.llvm.org/D135972



More information about the cfe-commits mailing list