Skip to content

Commit d908fe1

Browse files
committed
[clang][AST] 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 f1951f3 commit d908fe1

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
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

@@ -2133,6 +2139,12 @@ VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC,
21332139
"ParmVarDeclBitfields too large!");
21342140
static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned),
21352141
"NonParmVarDeclBitfields too large!");
2142+
2143+
// The unbraced `extern "C"` invariant is that the storage class
2144+
// specifier is omitted in the source code, i.e. SC_None (but is,
2145+
// implicitly, `extern`).
2146+
assert(!isUnbracedLanguageLinkage(DC) || SC == SC_None);
2147+
21362148
AllBits = 0;
21372149
VarDeclBits.SClass = SC;
21382150
// Everything else is implicitly initialized to false.
@@ -2316,7 +2328,7 @@ VarDecl::isThisDeclarationADefinition(ASTContext &C) const {
23162328
// A declaration directly contained in a linkage-specification is treated
23172329
// as if it contains the extern specifier for the purpose of determining
23182330
// the linkage of the declared name and whether it is a definition.
2319-
if (isSingleLineLanguageLinkage(*this))
2331+
if (hasUnbracedLanguageLinkage(*this))
23202332
return DeclarationOnly;
23212333

23222334
// C99 6.9.2p2:
@@ -3041,6 +3053,12 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
30413053
DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0),
30423054
EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) {
30433055
assert(T.isNull() || T->isFunctionType());
3056+
3057+
// The unbraced `extern "C"` invariant is that the storage class
3058+
// specifier is omitted in the source code, i.e. SC_None (but is,
3059+
// implicitly, `extern`).
3060+
assert(!isUnbracedLanguageLinkage(DC) || S == SC_None);
3061+
30443062
FunctionDeclBits.SClass = S;
30453063
FunctionDeclBits.IsInline = isInlineSpecified;
30463064
FunctionDeclBits.IsInlineSpecified = isInlineSpecified;

clang/lib/Sema/SemaDecl.cpp

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

23822382
FunctionDecl *New = FunctionDecl::Create(Context, Parent, Loc, Loc, II, Type,
2383-
/*TInfo=*/nullptr, SC_Extern,
2383+
/*TInfo=*/nullptr, SC_None,
23842384
getCurFPFeatures().isFPConstrained(),
23852385
false, Type->isFunctionProtoType());
23862386
New->setImplicit();

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6286,7 +6286,7 @@ static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context,
62866286
FunctionDecl *OverloadDecl = FunctionDecl::Create(
62876287
Context, Parent, FDecl->getLocation(), FDecl->getLocation(),
62886288
FDecl->getIdentifier(), OverloadTy,
6289-
/*TInfo=*/nullptr, SC_Extern, Sema->getCurFPFeatures().isFPConstrained(),
6289+
/*TInfo=*/nullptr, SC_None, Sema->getCurFPFeatures().isFPConstrained(),
62906290
false,
62916291
/*hasPrototype=*/true);
62926292
SmallVector<ParmVarDecl*, 16> Params;

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)