Skip to content

Conversation

@evelez7
Copy link
Member

@evelez7 evelez7 commented Jul 3, 2025

VarInfo was missing its addReference specialization. This causes errors
when a VarInfo is inside a namespace that isn't the global namespace.

Copy link
Member Author

evelez7 commented Jul 3, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@evelez7 evelez7 marked this pull request as ready for review July 3, 2025 21:36
@evelez7 evelez7 requested review from ilovepi and petrhosek July 3, 2025 21:36
@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Erick Velez (evelez7)

Changes

VarInfo was missing its addReference specialization. This causes errors
when a VarInfo is inside a namespace that isn't the global namespace.


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

2 Files Affected:

  • (modified) clang-tools-extra/clang-doc/BitcodeReader.cpp (+11)
  • (added) clang-tools-extra/test/clang-doc/json/nested-namespace.cpp (+36)
diff --git a/clang-tools-extra/clang-doc/BitcodeReader.cpp b/clang-tools-extra/clang-doc/BitcodeReader.cpp
index 2cbf8bf6b2879..f756ae6d897c8 100644
--- a/clang-tools-extra/clang-doc/BitcodeReader.cpp
+++ b/clang-tools-extra/clang-doc/BitcodeReader.cpp
@@ -569,6 +569,17 @@ static llvm::Error addReference(T I, Reference &&R, FieldId F) {
                                  "invalid type cannot contain Reference");
 }
 
+template <> llvm::Error addReference(VarInfo *I, Reference &&R, FieldId F) {
+  switch (F) {
+  case FieldId::F_namespace:
+    I->Namespace.emplace_back(std::move(R));
+    return llvm::Error::success();
+  default:
+    return llvm::createStringError(llvm::inconvertibleErrorCode(),
+                                   "VarInfo cannot contain this Reference");
+  }
+}
+
 template <> llvm::Error addReference(TypeInfo *I, Reference &&R, FieldId F) {
   switch (F) {
   case FieldId::F_type:
diff --git a/clang-tools-extra/test/clang-doc/json/nested-namespace.cpp b/clang-tools-extra/test/clang-doc/json/nested-namespace.cpp
new file mode 100644
index 0000000000000..5edfb34f34a13
--- /dev/null
+++ b/clang-tools-extra/test/clang-doc/json/nested-namespace.cpp
@@ -0,0 +1,36 @@
+// RUN: rm -rf %t && mkdir -p %t
+// RUN: clang-doc --output=%t --format=json --executor=standalone %s
+// RUN: FileCheck %s < %t/nested/index.json --check-prefix=NESTED
+// RUN: FileCheck %s < %t/nested/inner/index.json --check-prefix=INNER
+
+namespace nested {
+  int Global;
+  namespace inner {
+    int InnerGlobal;
+  } // namespace inner
+} // namespace nested
+
+// NESTED:       "Variables": [
+// NESTED-NEXT:    {
+// NESTED-NEXT:      "IsStatic": false,
+// NESTED-NEXT:      "Location": {
+// NESTED-NEXT:        "Filename": "{{.*}}nested-namespace.cpp",
+// NESTED-NEXT:        "LineNumber": 7
+// NESTED-NEXT:      },
+// NESTED-NEXT:      "Name": "Global",
+// NESTED-NEXT:      "Namespace": [
+// NESTED-NEXT:        "nested"
+// NESTED-NEXT:      ],
+
+// INNER:       "Variables": [
+// INNER-NEXT:    {
+// INNER-NEXT:      "IsStatic": false,
+// INNER-NEXT:      "Location": {
+// INNER-NEXT:        "Filename": "{{.*}}nested-namespace.cpp",
+// INNER-NEXT:        "LineNumber": 9
+// INNER-NEXT:      },
+// INNER-NEXT:      "Name": "InnerGlobal",
+// INNER-NEXT:      "Namespace": [
+// INNER-NEXT:        "inner",
+// INNER-NEXT:        "nested"
+// INNER-NEXT:      ],
\ No newline at end of file

VarInfo was missing its addReference specialization. This causes errors
when a VarInfo is inside a namespace that isn't the global namespace.
@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-fix-vars-namespaces branch from c686da5 to f505202 Compare July 3, 2025 21:53
Copy link
Member Author

evelez7 commented Jul 3, 2025

Merge activity

  • Jul 3, 10:49 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jul 3, 10:50 PM UTC: @evelez7 merged this pull request with Graphite.

@evelez7 evelez7 merged commit 0a02c28 into main Jul 3, 2025
9 checks passed
@evelez7 evelez7 deleted the users/evelez7/clang-doc-fix-vars-namespaces branch July 3, 2025 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants