diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index aa9fba519642aa..835e28c0bc9fc4 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -2216,14 +2216,18 @@ VarDecl *VarDecl::getActingDefinition() { return nullptr; VarDecl *LastTentative = nullptr; - VarDecl *First = getFirstDecl(); - for (auto I : First->redecls()) { - Kind = I->isThisDeclarationADefinition(); + + // Loop through the declaration chain, starting with the most recent. + for (VarDecl *Decl = getMostRecentDecl(); Decl; + Decl = Decl->getPreviousDecl()) { + Kind = Decl->isThisDeclarationADefinition(); if (Kind == Definition) return nullptr; - if (Kind == TentativeDefinition) - LastTentative = I; + // Record the first (most recent) TentativeDefinition that is encountered. + if (Kind == TentativeDefinition && !LastTentative) + LastTentative = Decl; } + return LastTentative; } diff --git a/clang/test/CodeGen/attr-tentative-definition.c b/clang/test/CodeGen/attr-tentative-definition.c new file mode 100644 index 00000000000000..7405e8079b220b --- /dev/null +++ b/clang/test/CodeGen/attr-tentative-definition.c @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -emit-llvm < %s | FileCheck %s + +char arr[10]; +char arr[10] __attribute__((section("datadata"))); +char arr[10] __attribute__((aligned(16))); + +// CHECK: @arr ={{.*}}section "datadata", align 16