https://github.com/temyurchenko created https://github.com/llvm/llvm-project/pull/93131
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. >From 10670795596129c33ae021a3970411b630637b80 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko <artemyurche...@zoho.com> Date: Wed, 22 May 2024 23:41:35 -0400 Subject: [PATCH] [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++"; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits