[cfe-commits] r83184 - in /cfe/trunk: include/clang/AST/Decl.h include/clang/AST/DeclBase.h lib/AST/DeclBase.cpp lib/CodeGen/CodeGenModule.cpp lib/CodeGen/Mangle.cpp lib/Sema/SemaDeclCXX.cpp test/CodeGenCXX/anonymous-namespaces.cpp

John McCall rjmccall at apple.com
Wed Sep 30 17:25:32 PDT 2009


Author: rjmccall
Date: Wed Sep 30 19:25:31 2009
New Revision: 83184

URL: http://llvm.org/viewvc/llvm-project?rev=83184&view=rev
Log:
Anonymous namespaces, sema + codegen.  A lot of semantics are still broken,
apparently because using directives aren't quite working correctly.


Added:
    cfe/trunk/test/CodeGenCXX/anonymous-namespaces.cpp
Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/DeclBase.h
    cfe/trunk/lib/AST/DeclBase.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/Mangle.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=83184&r1=83183&r2=83184&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Wed Sep 30 19:25:31 2009
@@ -192,6 +192,17 @@
 
   virtual void Destroy(ASTContext& C);
 
+  // \brief Returns true if this is an anonymous namespace declaration.
+  //
+  // For example:
+  //   namespace {
+  //     ...
+  //   };
+  // q.v. C++ [namespace.unnamed]
+  bool isAnonymousNamespace() const {
+    return !getIdentifier();
+  }
+
   NamespaceDecl *getNextNamespace() { return NextNamespace; }
   const NamespaceDecl *getNextNamespace() const { return NextNamespace; }
   void setNextNamespace(NamespaceDecl *ND) { NextNamespace = ND; }

Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=83184&r1=83183&r2=83184&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Wed Sep 30 19:25:31 2009
@@ -224,6 +224,8 @@
     return const_cast<Decl*>(this)->getTranslationUnitDecl();
   }
 
+  bool isInAnonymousNamespace() const;
+
   ASTContext &getASTContext() const;
 
   void setAccess(AccessSpecifier AS) {

Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=83184&r1=83183&r2=83184&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Wed Sep 30 19:25:31 2009
@@ -157,6 +157,17 @@
   }
 }
 
+bool Decl::isInAnonymousNamespace() const {
+  const DeclContext *DC = getDeclContext();
+  do {
+    if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
+      if (ND->isAnonymousNamespace())
+        return true;
+  } while ((DC = DC->getParent()));
+
+  return false;
+}
+
 TranslationUnitDecl *Decl::getTranslationUnitDecl() {
   if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
     return TUD;

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=83184&r1=83183&r2=83184&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Wed Sep 30 19:25:31 2009
@@ -247,6 +247,11 @@
 static CodeGenModule::GVALinkage
 GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD,
                       const LangOptions &Features) {
+  // Everything located semantically within an anonymous namespace is
+  // always internal.
+  if (FD->isInAnonymousNamespace())
+    return CodeGenModule::GVA_Internal;
+
   // The kind of external linkage this function will have, if it is not
   // inline or static.
   CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal;
@@ -1000,7 +1005,9 @@
   GV->setAlignment(getContext().getDeclAlignInBytes(D));
 
   // Set the llvm linkage type as appropriate.
-  if (D->getStorageClass() == VarDecl::Static)
+  if (D->isInAnonymousNamespace())
+    GV->setLinkage(llvm::Function::InternalLinkage);
+  else if (D->getStorageClass() == VarDecl::Static)
     GV->setLinkage(llvm::Function::InternalLinkage);
   else if (D->hasAttr<DLLImportAttr>())
     GV->setLinkage(llvm::Function::DLLImportLinkage);

Modified: cfe/trunk/lib/CodeGen/Mangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.cpp?rev=83184&r1=83183&r2=83184&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.cpp (original)
+++ cfe/trunk/lib/CodeGen/Mangle.cpp Wed Sep 30 19:25:31 2009
@@ -254,7 +254,8 @@
     return false;
 
   const NamespaceDecl *NS = cast<NamespaceDecl>(DC);
-  return NS->getOriginalNamespace()->getIdentifier()->isStr("std");
+  const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
+  return II && II->isStr("std");
 }
 
 static const TemplateDecl *
@@ -403,6 +404,14 @@
   DeclarationName Name = ND->getDeclName();
   switch (Name.getNameKind()) {
   case DeclarationName::Identifier:
+    if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND))
+      if (NS->isAnonymousNamespace()) {
+        // This is how gcc mangles these names.  It's apparently
+        // always '1', no matter how many different anonymous
+        // namespaces appear in a context.
+        Out << "12_GLOBAL__N_1";
+        break;
+      }
     mangleSourceName(Name.getAsIdentifierInfo());
     break;
 
@@ -1204,8 +1213,7 @@
 bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
   // <substitution> ::= St # ::std::
   if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
-    if (NS->getParent()->isTranslationUnit() &&
-        NS->getOriginalNamespace()->getIdentifier()->isStr("std")) {
+    if (isStdNamespace(NS)) {
       Out << "St";
       return true;
     }

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=83184&r1=83183&r2=83184&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Wed Sep 30 19:25:31 2009
@@ -2397,7 +2397,38 @@
 
     PushOnScopeChains(Namespc, DeclRegionScope);
   } else {
-    // FIXME: Handle anonymous namespaces
+    // Anonymous namespaces.
+
+    // C++ [namespace.unnamed]p1.  An unnamed-namespace-definition
+    //   behaves as if it were replaced by
+    //     namespace unique { /* empty body */ }
+    //     using namespace unique;
+    //     namespace unique { namespace-body }
+    //   where all occurrences of 'unique' in a translation unit are
+    //   replaced by the same identifier and this identifier differs
+    //   from all other identifiers in the entire program.
+
+    // We just create the namespace with an empty name and then add an
+    // implicit using declaration, just like the standard suggests.
+    //
+    // CodeGen enforces the "universally unique" aspect by giving all
+    // declarations semantically contained within an anonymous
+    // namespace internal linkage.
+
+    assert(Namespc->isAnonymousNamespace());
+    CurContext->addDecl(Namespc);
+
+    UsingDirectiveDecl* UD
+      = UsingDirectiveDecl::Create(Context, CurContext,
+                                   /* 'using' */ LBrace,
+                                   /* 'namespace' */ SourceLocation(),
+                                   /* qualifier */ SourceRange(),
+                                   /* NNS */ NULL,
+                                   /* identifier */ SourceLocation(),
+                                   Namespc,
+                                   /* Ancestor */ CurContext);
+    UD->setImplicit();
+    CurContext->addDecl(UD);
   }
 
   // Although we could have an invalid decl (i.e. the namespace name is a

Added: cfe/trunk/test/CodeGenCXX/anonymous-namespaces.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/anonymous-namespaces.cpp?rev=83184&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenCXX/anonymous-namespaces.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/anonymous-namespaces.cpp Wed Sep 30 19:25:31 2009
@@ -0,0 +1,22 @@
+// RUN: clang-cc -emit-llvm %s -o - | FileCheck %s
+
+namespace {
+  // CHECK: @_ZN12_GLOBAL__N_11aE = internal global i32 0
+  int a = 0;
+
+  // CHECK: define internal i32 @_ZN12_GLOBAL__N_13fooEv()
+  int foo() {
+    return 32;
+  }
+
+  // CHECK: define internal i32 @_ZN12_GLOBAL__N_11A3fooEv()
+  namespace A {
+    int foo() {
+      return 45;
+    }
+  }
+}
+
+int concrete() {
+  return a + foo() + A::foo();
+}





More information about the cfe-commits mailing list