[clang] [clang][AST] fix ast-print of `extern <lang>` with >=2 declarators (PR #93131)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 22 21:06:40 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Artem Yurchenko (temyurchenko)
<details>
<summary>Changes</summary>
Problem: the printer used to ignore all but the first declarator for unbraced language linkage declarators. Furthemore, that one would be printed without the final semicolon.
Solution: when there is more than one declarator, we print them in a braced `extern <lang>` block. If the original declaration was unbraced and there is one or less declarator, we omit the braces, but add the semicolon.
**N.B.** We are printing braces which were, in some cases, absent from the original CST. If that's an issue, I'll work on it. See the tests for the examples.
---
Full diff: https://github.com/llvm/llvm-project/pull/93131.diff
2 Files Affected:
- (modified) clang/lib/AST/DeclPrinter.cpp (+4-2)
- (added) clang/test/AST/ast-print-language-linkage.cpp (+27)
``````````diff
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 0cf4e64f83b8d..369dfffe58667 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -1145,13 +1145,15 @@ void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
l = "C++";
}
+ bool HasMoreThanOneDecl =
+ *D->decls_begin() && D->decls_begin()->getNextDeclInContext();
Out << "extern \"" << l << "\" ";
- if (D->hasBraces()) {
+ if (D->hasBraces() || HasMoreThanOneDecl) {
Out << "{\n";
VisitDeclContext(D);
Indent() << "}";
} else
- Visit(*D->decls_begin());
+ VisitDeclContext(D);
}
void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
diff --git a/clang/test/AST/ast-print-language-linkage.cpp b/clang/test/AST/ast-print-language-linkage.cpp
new file mode 100644
index 0000000000000..4998a62f280b5
--- /dev/null
+++ b/clang/test/AST/ast-print-language-linkage.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -ast-print %s -o - | FileCheck %s
+
+// CHECK: extern "C" int printf(const char *, ...);
+extern "C" int printf(const char *...);
+
+// CHECK: extern "C++" {
+// CHECK-NEXT: int f(int);
+// CHECK-NEXT: int g(int);
+// CHECK-NEXT: }
+extern "C++" int f(int), g(int);
+
+// CHECK: extern "C" {
+// CHECK-NEXT: void foo();
+// CHECK-NEXT: int x;
+// CHECK-NEXT: int y;
+// CHECK-NEXT: }
+extern "C" {
+ void foo(void);
+ int x, y;
+}
+
+// CHECK: extern "C" {
+// CHECK-NEXT: }
+extern "C" {}
+
+// CHECK: extern "C++" ;
+extern "C++";
``````````
</details>
https://github.com/llvm/llvm-project/pull/93131
More information about the cfe-commits
mailing list