[clang] [clang][AST] fix ast-print of `extern <lang>` with >=2 declarators (PR #93131)
Artem Yurchenko via cfe-commits
cfe-commits at lists.llvm.org
Thu May 23 11:15:52 PDT 2024
https://github.com/temyurchenko updated https://github.com/llvm/llvm-project/pull/93131
>From 10670795596129c33ae021a3970411b630637b80 Mon Sep 17 00:00:00 2001
From: Artem Yurchenko <artemyurchenko at zoho.com>
Date: Wed, 22 May 2024 23:41:35 -0400
Subject: [PATCH 1/2] [clang][AST] fix ast-print of `extern <lang>` with >=2
declarators
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.
---
clang/lib/AST/DeclPrinter.cpp | 6 +++--
clang/test/AST/ast-print-language-linkage.cpp | 27 +++++++++++++++++++
2 files changed, 31 insertions(+), 2 deletions(-)
create mode 100644 clang/test/AST/ast-print-language-linkage.cpp
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++";
>From b4204dff09bd5fc47edf1f136d512db7570e4ad7 Mon Sep 17 00:00:00 2001
From: Artem Yurchenko <artemyurchenko at zoho.com>
Date: Thu, 23 May 2024 14:15:31 -0400
Subject: [PATCH 2/2] fixup! [clang][AST] fix ast-print of `extern <lang>` with
>=2 declarators
---
clang/lib/AST/DeclPrinter.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 369dfffe58667..131d2ac37bb7e 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -1145,8 +1145,7 @@ void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
l = "C++";
}
- bool HasMoreThanOneDecl =
- *D->decls_begin() && D->decls_begin()->getNextDeclInContext();
+ bool HasMoreThanOneDecl = std::distance(D->decls_begin(), D->decls_end()) > 1;
Out << "extern \"" << l << "\" ";
if (D->hasBraces() || HasMoreThanOneDecl) {
Out << "{\n";
More information about the cfe-commits
mailing list