[PATCH] D135422: Fix clang-format misattributing preprocessor directives to macros

Jacob Abraham via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 6 22:19:16 PDT 2022


jacob-abraham created this revision.
jacob-abraham added reviewers: djasper, Typz.
jacob-abraham added a project: clang-format.
Herald added a project: All.
jacob-abraham requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This solves the issue I documented at https://github.com/llvm/llvm-project/issues/58214.

Essentially, a case statement inside a macro greedily adds preprocessor lines such as `#include` to the macro, even if they are not a part of the macro to begin with. Short quick fix. I tried to do it cleanly, but this was my first attempt at patching clang-format so if there is a cleaner/better way to fix this that would be awesome!


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135422

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp


Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -643,6 +643,7 @@
     unsigned Length = 0;
     bool EndsWithComment = false;
     bool InPPDirective = I[0]->InPPDirective;
+    bool InMacroBody = I[0]->InMacroBody;
     const unsigned Level = I[0]->Level;
     for (; NumStmts < 3; ++NumStmts) {
       if (I + 1 + NumStmts == E)
@@ -650,6 +651,8 @@
       const AnnotatedLine *Line = I[1 + NumStmts];
       if (Line->InPPDirective != InPPDirective)
         break;
+      if(Line->InMacroBody != InMacroBody)
+        break;
       if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
         break;
       if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135422.465969.patch
Type: text/x-patch
Size: 865 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221007/1962e70c/attachment.bin>


More information about the cfe-commits mailing list