[clang-tools-extra] [clang-doc] simplify filename selection for namespaces (PR #162885)

Erick Velez via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 10 10:00:10 PDT 2025


https://github.com/evelez7 created https://github.com/llvm/llvm-project/pull/162885

determineFileName was confusing regarding namespaces. The comment and
conditional were both misleading. Now, we just check against a static
global namespace USR to make a file use "index", or just use the name.

>From b345c722a6d9d678220631010860c99ae110f460 Mon Sep 17 00:00:00 2001
From: Erick Velez <erickvelez7 at gmail.com>
Date: Wed, 8 Oct 2025 18:50:41 -0700
Subject: [PATCH] [clang-doc] simplify filename selection for namespaces

determineFileName was confusing regarding namespaces. The comment and
conditional were both misleading. Now, we just check against a static
global namespace USR to make a file use "index", or just use the name.
---
 clang-tools-extra/clang-doc/JSONGenerator.cpp | 7 +++----
 clang-tools-extra/clang-doc/Representation.h  | 3 +++
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-doc/JSONGenerator.cpp b/clang-tools-extra/clang-doc/JSONGenerator.cpp
index 26794a5e34d02..1b08b1791b6eb 100644
--- a/clang-tools-extra/clang-doc/JSONGenerator.cpp
+++ b/clang-tools-extra/clang-doc/JSONGenerator.cpp
@@ -582,11 +582,10 @@ static SmallString<16> determineFileName(Info *I, SmallString<128> &Path) {
   if (I->IT == InfoType::IT_record) {
     auto *RecordSymbolInfo = static_cast<SymbolInfo *>(I);
     FileName = RecordSymbolInfo->MangledName;
-  } else if (I->IT == InfoType::IT_namespace && I->Name != "")
-    // Serialize the global namespace as index.json
-    FileName = I->Name;
+  } else if (I->USR == GlobalNamespace)
+    FileName = "index";
   else
-    FileName = I->getFileBaseName();
+    FileName = I->Name;
   sys::path::append(Path, FileName + ".json");
   return FileName;
 }
diff --git a/clang-tools-extra/clang-doc/Representation.h b/clang-tools-extra/clang-doc/Representation.h
index 2a75f89696b7d..4dd7ce86b9304 100644
--- a/clang-tools-extra/clang-doc/Representation.h
+++ b/clang-tools-extra/clang-doc/Representation.h
@@ -30,6 +30,9 @@ namespace doc {
 // SHA1'd hash of a USR.
 using SymbolID = std::array<uint8_t, 20>;
 
+static const SymbolID GlobalNamespace = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+
 struct BaseRecordInfo;
 struct EnumInfo;
 struct FunctionInfo;



More information about the cfe-commits mailing list