Author: Ilya Biryukov
Date: 2025-08-01T14:26:17+02:00
New Revision: 803a50aa0547fdbcd661c54f19de0c155a23d50d

URL: 
https://github.com/llvm/llvm-project/commit/803a50aa0547fdbcd661c54f19de0c155a23d50d
DIFF: 
https://github.com/llvm/llvm-project/commit/803a50aa0547fdbcd661c54f19de0c155a23d50d.diff

LOG: [Clang] Dump minimization hints for namespaces (#151534)

Unlike other declarations, these cover two ranges:
- from `namespace/inline namespace` to the opening `{`,
- the closing `}`.

This allows to mark the declarations inside the namespace itself
independently.

Added: 
    

Modified: 
    clang/lib/Frontend/FrontendAction.cpp
    clang/test/Frontend/dump-minimization-hints.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/FrontendAction.cpp 
b/clang/lib/Frontend/FrontendAction.cpp
index 12b5c1837ded6..87cc2fc0cdd17 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -9,6 +9,7 @@
 #include "clang/Frontend/FrontendAction.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/Builtins.h"
 #include "clang/Basic/DiagnosticOptions.h"
@@ -39,6 +40,7 @@
 #include "clang/Serialization/ASTReader.h"
 #include "clang/Serialization/GlobalModuleIndex.h"
 #include "llvm/ADT/ScopeExit.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/BuryPointer.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -87,12 +89,25 @@ class DeserializedDeclsSourceRangePrinter : public 
ASTConsumer,
       // reducing the granularity and making the output less useful.
       return;
     }
-    if (auto *DC = D->getDeclContext(); !DC || !DC->isFileContext()) {
+    auto *DC = D->getLexicalDeclContext();
+    if (!DC || !DC->isFileContext()) {
       // We choose to work at namespace level to reduce complexity and the
       // number of cases we care about.
       return;
     }
+
     PendingDecls.push_back(D);
+    if (auto *NS = dyn_cast<NamespaceDecl>(DC)) {
+      // Add any namespaces we have not seen before.
+      // Note that we filter out namespaces from DeclRead as it includes too
+      // all redeclarations and we only want the ones that had other used
+      // declarations.
+      while (NS && ProcessedNamespaces.insert(NS).second) {
+        PendingDecls.push_back(NS);
+
+        NS = dyn_cast<NamespaceDecl>(NS->getLexicalParent());
+      }
+    }
   }
 
   struct Position {
@@ -141,23 +156,25 @@ class DeserializedDeclsSourceRangePrinter : public 
ASTConsumer,
       OptionalFileEntryRef Ref;
     };
     llvm::DenseMap<const FileEntry *, FileData> FileToRanges;
+
     for (const Decl *D : PendingDecls) {
-      CharSourceRange R = SM.getExpansionRange(D->getSourceRange());
-      if (!R.isValid())
-        continue;
+      for (CharSourceRange R : getRangesToMark(D)) {
+        if (!R.isValid())
+          continue;
 
-      auto *F = SM.getFileEntryForID(SM.getFileID(R.getBegin()));
-      if (F != SM.getFileEntryForID(SM.getFileID(R.getEnd()))) {
-        // Such cases are rare and 
diff icult to handle.
-        continue;
-      }
+        auto *F = SM.getFileEntryForID(SM.getFileID(R.getBegin()));
+        if (F != SM.getFileEntryForID(SM.getFileID(R.getEnd()))) {
+          // Such cases are rare and 
diff icult to handle.
+          continue;
+        }
 
-      auto &Data = FileToRanges[F];
-      if (!Data.Ref)
-        Data.Ref = SM.getFileEntryRefForID(SM.getFileID(R.getBegin()));
-      Data.FromTo.push_back(
-          {Position::GetBeginSpelling(SM, R),
-           Position::GetEndSpelling(SM, R, D->getLangOpts())});
+        auto &Data = FileToRanges[F];
+        if (!Data.Ref)
+          Data.Ref = SM.getFileEntryRefForID(SM.getFileID(R.getBegin()));
+        Data.FromTo.push_back(
+            {Position::GetBeginSpelling(SM, R),
+             Position::GetEndSpelling(SM, R, D->getLangOpts())});
+      }
     }
 
     // To simplify output, merge consecutive and intersecting ranges.
@@ -188,10 +205,49 @@ class DeserializedDeclsSourceRangePrinter : public 
ASTConsumer,
 
 private:
   std::vector<const Decl *> PendingDecls;
+  llvm::SmallPtrSet<const NamespaceDecl *, 0> ProcessedNamespaces;
   bool IsCollectingDecls = true;
   const SourceManager &SM;
   std::unique_ptr<llvm::raw_ostream> OS;
 
+  llvm::SmallVector<CharSourceRange, 2> getRangesToMark(const Decl *D) {
+    auto *NS = dyn_cast<NamespaceDecl>(D);
+    if (!NS)
+      return {SM.getExpansionRange(D->getSourceRange())};
+
+    SourceLocation LBraceLoc;
+    if (NS->isAnonymousNamespace()) {
+      LBraceLoc = NS->getLocation();
+    } else {
+      // Start with the location of the identifier.
+      SourceLocation TokenBeforeLBrace = NS->getLocation();
+      if (NS->hasAttrs()) {
+        for (auto *A : NS->getAttrs()) {
+          // But attributes may go after it.
+          if (SM.isBeforeInTranslationUnit(TokenBeforeLBrace,
+                                           A->getRange().getEnd())) {
+            // Give up, the attributes are often coming from macros and we
+            // cannot skip them reliably.
+            return {};
+          }
+        }
+      }
+      auto &LangOpts = D->getLangOpts();
+      // Now skip one token, the next should be the lbrace.
+      Token Tok;
+      if (Lexer::getRawToken(TokenBeforeLBrace, Tok, SM, LangOpts, true) ||
+          Lexer::getRawToken(Tok.getEndLoc(), Tok, SM, LangOpts, true) ||
+          Tok.getKind() != tok::l_brace) {
+        // On error or if we did not find the token we expected, avoid marking
+        // everything inside the namespace as used.
+        return {};
+      }
+      LBraceLoc = Tok.getLocation();
+    }
+    return {SM.getExpansionRange(SourceRange(NS->getBeginLoc(), LBraceLoc)),
+            SM.getExpansionRange(NS->getRBraceLoc())};
+  }
+
   void printJson(llvm::ArrayRef<RequiredRanges> Result) {
     *OS << "{\n";
     *OS << R"(  "required_ranges": [)" << "\n";

diff  --git a/clang/test/Frontend/dump-minimization-hints.cpp 
b/clang/test/Frontend/dump-minimization-hints.cpp
index 273fd7f4ecd63..4c5dfbc343cd4 100644
--- a/clang/test/Frontend/dump-minimization-hints.cpp
+++ b/clang/test/Frontend/dump-minimization-hints.cpp
@@ -59,6 +59,36 @@
 // RANGE-NEXT:            "line": 23,
 // RANGE-NEXT:            "column": 2
 // RANGE-NEXT:          }
+// RANGE-NEXT:        },
+// RANGE-NEXT:        {
+// RANGE-NEXT:          "from": {
+// RANGE-NEXT:            "line": 31,
+// RANGE-NEXT:            "column": 1
+// RANGE-NEXT:          },
+// RANGE-NEXT:          "to": {
+// RANGE-NEXT:            "line": 31,
+// RANGE-NEXT:            "column": 27
+// RANGE-NEXT:          }
+// RANGE-NEXT:        },
+// RANGE-NEXT:        {
+// RANGE-NEXT:          "from": {
+// RANGE-NEXT:            "line": 32,
+// RANGE-NEXT:            "column": 3
+// RANGE-NEXT:          },
+// RANGE-NEXT:          "to": {
+// RANGE-NEXT:            "line": 32,
+// RANGE-NEXT:            "column": 12
+// RANGE-NEXT:          }
+// RANGE-NEXT:        },
+// RANGE-NEXT:        {
+// RANGE-NEXT:          "from": {
+// RANGE-NEXT:            "line": 34,
+// RANGE-NEXT:            "column": 1
+// RANGE-NEXT:          },
+// RANGE-NEXT:          "to": {
+// RANGE-NEXT:            "line": 34,
+// RANGE-NEXT:            "column": 2
+// RANGE-NEXT:          }
 // RANGE-NEXT:        }
 // RANGE-NEXT:      ]
 // RANGE-NEXT:    }
@@ -88,7 +118,7 @@ int multiply(int a, int b) {
     return a * b;
 }
 
-inline int unused_by_foo() {} // line 17
+inline void unused_by_foo() {} // line 17
 
 inline void recursively_used_by_foo() {} // line 19
 inline int used_by_foo() { // line 20
@@ -98,6 +128,20 @@ inline int used_by_foo() { // line 20
 
 struct UnusedByFoo {};
 
+namespace ns_unused_by_foo {
+  void x();
+}
+
+namespace ns_used_by_foo { // line 31
+  void x(); // line 32
+  void unused_y(); 
+} // line 34
+
+// Does not have any declarations that are used, so 
+// will not be marked as used.
+namespace ns_used_by_foo { 
+  void unused_z();
+}
 //--- foo.cpp
 #include "foo.h"
 int global_value = 5;
@@ -107,5 +151,6 @@ int main() {
   int doubled_value = multiply(current_value, 2);
   int final_result = doubled_value + global_value;
 
-  return used_by_foo();
+  used_by_foo();
+  ns_used_by_foo::x();
 }


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to