[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) {
+      if (First) {
+        First = false;
+      } else {
+        Range = {Last, B.first.Tokens.Begin};
+        Ranges.push_back(Range);
+      }
+      Last = B.first.Tokens.Begin;
+    }
+
+    if (C.End.Kind != tok::pp_not_keyword) {
+      Range = {Last, C.End.Tokens.Begin};
+      Ranges.push_back(Range);
+    }
+
+    for (const auto &B : C.Branches)
----------------
HighCommander4 wrote:

Likewise

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


More information about the cfe-commits mailing list