Skip to content

Revert "[clang][AST] fix ast-print of extern <lang> with >=2 declarators" #93912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2024

Conversation

gulfemsavrun
Copy link
Contributor

Reverts #93131 because it broke some lldb tests on the Fuchsia Clang toolchain builders.
https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-x64/b8746482644341901905/infra

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 31, 2024
@gulfemsavrun gulfemsavrun merged commit aa98c75 into main May 31, 2024
5 of 9 checks passed
@gulfemsavrun gulfemsavrun deleted the revert-93131-main branch May 31, 2024 01:51
@llvmbot
Copy link
Member

llvmbot commented May 31, 2024

@llvm/pr-subscribers-clang

Author: None (gulfemsavrun)

Changes

Reverts llvm/llvm-project#93131 because it broke some lldb tests on the Fuchsia Clang toolchain builders.
https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-x64/b8746482644341901905/infra


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

2 Files Affected:

  • (modified) clang/lib/AST/DeclPrinter.cpp (+14-41)
  • (removed) clang/test/AST/ast-print-language-linkage.cpp (-31)
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 9250a7f6eceb2..0cf4e64f83b8d 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -633,7 +633,7 @@ static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
   Out << Proto;
 }
 
-static void maybePrintTagKeywordIfSupressingScopes(PrintingPolicy &Policy,
+static void MaybePrintTagKeywordIfSupressingScopes(PrintingPolicy &Policy,
                                                    QualType T,
                                                    llvm::raw_ostream &Out) {
   StringRef prefix = T->isClassType()       ? "class "
@@ -643,22 +643,6 @@ static void maybePrintTagKeywordIfSupressingScopes(PrintingPolicy &Policy,
   Out << prefix;
 }
 
-/// Return the language of the linkage spec of `D`, if applicable.
-///
-/// \Return - "C" if `D` has been declared with unbraced `extern "C"`
-///         - "C++" if `D` has been declared with unbraced `extern "C++"`
-///         - nullptr in any other case
-static const char *tryGetUnbracedLinkageLanguage(const Decl *D) {
-  const auto *SD = dyn_cast<LinkageSpecDecl>(D->getDeclContext());
-  if (!SD || SD->hasBraces())
-    return nullptr;
-  if (SD->getLanguage() == LinkageSpecLanguageIDs::C)
-    return "C";
-  assert(SD->getLanguage() == LinkageSpecLanguageIDs::CXX &&
-         "unknown language in linkage specification");
-  return "C++";
-}
-
 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
   if (!D->getDescribedFunctionTemplate() &&
       !D->isFunctionTemplateSpecialization()) {
@@ -678,11 +662,6 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
   CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
   CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
   if (!Policy.SuppressSpecifiers) {
-    if (const char *Lang = tryGetUnbracedLinkageLanguage(D)) {
-      // the "extern" specifier is implicit
-      assert(D->getStorageClass() == SC_None);
-      Out << "extern \"" << Lang << "\" ";
-    }
     switch (D->getStorageClass()) {
     case SC_None: break;
     case SC_Extern: Out << "extern "; break;
@@ -828,7 +807,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
       }
       if (!Policy.SuppressTagKeyword && Policy.SuppressScope &&
           !Policy.SuppressUnwrittenScope)
-        maybePrintTagKeywordIfSupressingScopes(Policy, AFT->getReturnType(),
+        MaybePrintTagKeywordIfSupressingScopes(Policy, AFT->getReturnType(),
                                                Out);
       AFT->getReturnType().print(Out, Policy, Proto);
       Proto.clear();
@@ -953,11 +932,6 @@ void DeclPrinter::VisitVarDecl(VarDecl *D) {
     : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
 
   if (!Policy.SuppressSpecifiers) {
-    if (const char *Lang = tryGetUnbracedLinkageLanguage(D)) {
-      // the "extern" specifier is implicit
-      assert(D->getStorageClass() == SC_None);
-      Out << "extern \"" << Lang << "\" ";
-    }
     StorageClass SC = D->getStorageClass();
     if (SC != SC_None)
       Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
@@ -987,7 +961,7 @@ void DeclPrinter::VisitVarDecl(VarDecl *D) {
 
   if (!Policy.SuppressTagKeyword && Policy.SuppressScope &&
       !Policy.SuppressUnwrittenScope)
-    maybePrintTagKeywordIfSupressingScopes(Policy, T, Out);
+    MaybePrintTagKeywordIfSupressingScopes(Policy, T, Out);
 
   printDeclType(T, (isa<ParmVarDecl>(D) && Policy.CleanUglifiedParameters &&
                     D->getIdentifier())
@@ -1090,8 +1064,6 @@ void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
 
 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
   prettyPrintAttributes(D);
-  if (const char *Lang = tryGetUnbracedLinkageLanguage(D))
-    Out << "extern \"" << Lang << "\";";
 }
 
 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
@@ -1164,21 +1136,22 @@ void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
 }
 
 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
-  if (!D->hasBraces()) {
-    VisitDeclContext(D);
-    return;
-  }
-  const char *L;
+  const char *l;
   if (D->getLanguage() == LinkageSpecLanguageIDs::C)
-    L = "C";
+    l = "C";
   else {
     assert(D->getLanguage() == LinkageSpecLanguageIDs::CXX &&
            "unknown language in linkage specification");
-    L = "C++";
+    l = "C++";
   }
-  Out << "extern \"" << L << "\" {\n";
-  VisitDeclContext(D);
-  Indent() << "}";
+
+  Out << "extern \"" << l << "\" ";
+  if (D->hasBraces()) {
+    Out << "{\n";
+    VisitDeclContext(D);
+    Indent() << "}";
+  } else
+    Visit(*D->decls_begin());
 }
 
 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
deleted file mode 100644
index 7e4dc3f25f062..0000000000000
--- a/clang/test/AST/ast-print-language-linkage.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-// 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++" int f(int);
-// CHECK-NEXT: extern "C++" int g(int);
-extern "C++" int f(int), g(int);
-
-// CHECK: extern "C" char a;
-// CHECK-NEXT: extern "C" char b;
-extern "C" char a, b;
-
-// CHECK: extern "C" {
-// CHECK-NEXT:  void foo();
-// CHECK-NEXT:  int x;
-// CHECK-NEXT:  int y;
-// CHECK-NEXT:  extern short z;
-// CHECK-NEXT: }
-extern "C" {
-  void foo(void);
-  int x, y;
-  extern short z;
-}
-
-// CHECK: extern "C" {
-// CHECK-NEXT: }
-extern "C" {}
-
-// CHECK: extern "C++";
-extern "C++";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants