[clang-tools-extra] [clangd] Implement simple folding for preprocessor branches (PR #140959)

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 24 17:56:05 PDT 2025


================
@@ -356,5 +356,62 @@ TokenStream DirectiveTree::stripDirectives(const TokenStream &In) const {
   return Out;
 }
 
+namespace {
+class RangePairer {
+  std::vector<Token::Range> &Ranges;
+
+public:
+  RangePairer(std::vector<Token::Range> &Ranges) : Ranges(Ranges) {}
+
+  void walk(const DirectiveTree &T) {
+    for (const auto &C : T.Chunks)
+      std::visit(*this, C);
+  }
+
+  void operator()(const DirectiveTree::Code &C) {}
+
+  void operator()(const DirectiveTree::Directive &) {}
+
+  void operator()(const DirectiveTree::Conditional &C) {
+    Token::Range Range;
+    Token::Index Last;
+    auto First = true;
+    for (const auto &B : C.Branches) {
----------------
HighCommander4 wrote:

Consider using a structured binding here:

```c++
for (const auto &[Directive, _] : C.Branches) {
  // use Directive in place of B.first
}
```

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


More information about the cfe-commits mailing list