Skip to content

Commit 5a0cdc1

Browse files
committed
enforce the unbraced extern <lang> invariant
Quoting 9.11.8: > A declaration directly contained in a linkage-specification is > treated as if it contains the extern specifier (9.2.2) for the > purpose of determining the linkage of the declared name and whether > it is a definition. Such a declaration shall not specify a storage > class. So, the invariant is that function and variable declarations within an unbraced language linkage specification have `StorageClass` set to `SC_None`. Problem: in several places the invariant was broken. Solution: remove the explicit `SC_Extern` specification. Note, that my changes result in the `extern` being implicit in some functions that are not contained in a language linkage specification.
1 parent fac4547 commit 5a0cdc1

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

clang/lib/AST/Decl.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,19 @@ template <typename T> static bool isFirstInExternCContext(T *D) {
576576
return First->isInExternCContext();
577577
}
578578

579-
static bool isSingleLineLanguageLinkage(const Decl &D) {
580-
if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
579+
static bool isUnbracedLanguageLinkage(const DeclContext *DC) {
580+
if (!DC)
581+
return false;
582+
if (const auto *SD = dyn_cast<LinkageSpecDecl>(DC))
581583
if (!SD->hasBraces())
582584
return true;
583585
return false;
584586
}
585587

588+
static bool hasUnbracedLanguageLinkage(const Decl &D) {
589+
return isUnbracedLanguageLinkage(D.getDeclContext());
590+
}
591+
586592
static bool isDeclaredInModuleInterfaceOrPartition(const NamedDecl *D) {
587593
if (auto *M = D->getOwningModule())
588594
return M->isInterfaceOrPartition();
@@ -644,7 +650,7 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
644650

645651
if (Var->getStorageClass() != SC_Extern &&
646652
Var->getStorageClass() != SC_PrivateExtern &&
647-
!isSingleLineLanguageLinkage(*Var))
653+
!hasUnbracedLanguageLinkage(*Var))
648654
return LinkageInfo::internal();
649655
}
650656

@@ -2140,6 +2146,12 @@ VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC,
21402146
"ParmVarDeclBitfields too large!");
21412147
static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned),
21422148
"NonParmVarDeclBitfields too large!");
2149+
2150+
// The unbraced `extern "C"` invariant is that the storage class
2151+
// specifier is omitted in the source code, i.e. SC_None (but is,
2152+
// implicitly, `extern`).
2153+
assert(!isUnbracedLanguageLinkage(DC) || SC == SC_None);
2154+
21432155
AllBits = 0;
21442156
VarDeclBits.SClass = SC;
21452157
// Everything else is implicitly initialized to false.
@@ -2323,7 +2335,7 @@ VarDecl::isThisDeclarationADefinition(ASTContext &C) const {
23232335
// A declaration directly contained in a linkage-specification is treated
23242336
// as if it contains the extern specifier for the purpose of determining
23252337
// the linkage of the declared name and whether it is a definition.
2326-
if (isSingleLineLanguageLinkage(*this))
2338+
if (hasUnbracedLanguageLinkage(*this))
23272339
return DeclarationOnly;
23282340

23292341
// C99 6.9.2p2:
@@ -3046,6 +3058,12 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
30463058
DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0),
30473059
EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) {
30483060
assert(T.isNull() || T->isFunctionType());
3061+
3062+
// The unbraced `extern "C"` invariant is that the storage class
3063+
// specifier is omitted in the source code, i.e. SC_None (but is,
3064+
// implicitly, `extern`).
3065+
assert(!isUnbracedLanguageLinkage(DC) || S == SC_None);
3066+
30493067
FunctionDeclBits.SClass = S;
30503068
FunctionDeclBits.IsInline = isInlineSpecified;
30513069
FunctionDeclBits.IsInlineSpecified = isInlineSpecified;

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ FunctionDecl *Sema::CreateBuiltin(IdentifierInfo *II, QualType Type,
23742374
}
23752375

23762376
FunctionDecl *New = FunctionDecl::Create(Context, Parent, Loc, Loc, II, Type,
2377-
/*TInfo=*/nullptr, SC_Extern,
2377+
/*TInfo=*/nullptr, SC_None,
23782378
getCurFPFeatures().isFPConstrained(),
23792379
false, Type->isFunctionProtoType());
23802380
New->setImplicit();

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ class CodeComplete : public CodeCompleteConsumer {
881881
else
882882
ToInsert += "(";
883883
raw_string_ostream OS(Description);
884-
F->print(OS, m_desc_policy, false);
884+
F->print(OS, m_desc_policy);
885885
OS.flush();
886886
} else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
887887
Description = V->getType().getAsString(m_desc_policy);

lldb/source/Plugins/ExpressionParser/Clang/NameSearchContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ clang::NamedDecl *NameSearchContext::AddFunDecl(const CompilerType &type,
7777

7878
clang::FunctionDecl *func_decl = FunctionDecl::Create(
7979
ast, context, SourceLocation(), SourceLocation(), decl_name, qual_type,
80-
nullptr, SC_Extern, /*UsesFPIntrin=*/false, isInlineSpecified, hasWrittenPrototype,
80+
nullptr, SC_None, /*UsesFPIntrin=*/false, isInlineSpecified,
81+
hasWrittenPrototype,
8182
isConstexprSpecified ? ConstexprSpecKind::Constexpr
8283
: ConstexprSpecKind::Unspecified);
8384

0 commit comments

Comments
 (0)