[clang] [clang-format] Fix crashes when the macro expansion is empty (PR #119428)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 10 10:23:07 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: 天音あめ (amane-ame)

<details>
<summary>Changes</summary>

An empty expansion should be valid, like `echo 'A()' | clang-format --style='{Macros: [A(x)=x]}'`
See #<!-- -->119258.

---
Full diff: https://github.com/llvm/llvm-project/pull/119428.diff


1 Files Affected:

- (modified) clang/lib/Format/MacroExpander.cpp (+4) 


``````````diff
diff --git a/clang/lib/Format/MacroExpander.cpp b/clang/lib/Format/MacroExpander.cpp
index fd2a16894d643d..ed9e51dfbfef1f 100644
--- a/clang/lib/Format/MacroExpander.cpp
+++ b/clang/lib/Format/MacroExpander.cpp
@@ -233,6 +233,10 @@ MacroExpander::expand(FormatToken *ID,
   if (Result.size() > 1) {
     ++Result[0]->MacroCtx->StartOfExpansion;
     ++Result[Result.size() - 2]->MacroCtx->EndOfExpansion;
+  } else {
+    // If the macro expansion is empty, mark the start and end
+    Result[0]->MacroCtx->StartOfExpansion = 1;
+    Result[0]->MacroCtx->EndOfExpansion = 1;
   }
   return Result;
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/119428


More information about the cfe-commits mailing list