Skip to content

Commit b9fa7fe

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 410c7ba commit b9fa7fe

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

clang/lib/AST/Decl.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,16 @@ 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()))
581-
if (!SD->hasBraces())
582-
return true;
579+
static bool isUnbracedLanguageLinkage(const DeclContext *DC) {
580+
if (const auto *SD = dyn_cast_if_present<LinkageSpecDecl>(DC))
581+
return !SD->hasBraces();
583582
return false;
584583
}
585584

585+
static bool hasUnbracedLanguageLinkage(const Decl &D) {
586+
return isUnbracedLanguageLinkage(D.getDeclContext());
587+
}
588+
586589
static bool isDeclaredInModuleInterfaceOrPartition(const NamedDecl *D) {
587590
if (auto *M = D->getOwningModule())
588591
return M->isInterfaceOrPartition();
@@ -644,7 +647,7 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
644647

645648
if (Var->getStorageClass() != SC_Extern &&
646649
Var->getStorageClass() != SC_PrivateExtern &&
647-
!isSingleLineLanguageLinkage(*Var))
650+
!hasUnbracedLanguageLinkage(*Var))
648651
return LinkageInfo::internal();
649652
}
650653

@@ -2133,6 +2136,12 @@ VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC,
21332136
"ParmVarDeclBitfields too large!");
21342137
static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned),
21352138
"NonParmVarDeclBitfields too large!");
2139+
2140+
// The unbraced `extern "C"` invariant is that the storage class
2141+
// specifier is omitted in the source code, i.e. SC_None (but is,
2142+
// implicitly, `extern`).
2143+
assert(!isUnbracedLanguageLinkage(DC) || SC == SC_None);
2144+
21362145
AllBits = 0;
21372146
VarDeclBits.SClass = SC;
21382147
// Everything else is implicitly initialized to false.
@@ -2316,7 +2325,7 @@ VarDecl::isThisDeclarationADefinition(ASTContext &C) const {
23162325
// A declaration directly contained in a linkage-specification is treated
23172326
// as if it contains the extern specifier for the purpose of determining
23182327
// the linkage of the declared name and whether it is a definition.
2319-
if (isSingleLineLanguageLinkage(*this))
2328+
if (hasUnbracedLanguageLinkage(*this))
23202329
return DeclarationOnly;
23212330

23222331
// C99 6.9.2p2:
@@ -3041,6 +3050,12 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
30413050
DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0),
30423051
EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) {
30433052
assert(T.isNull() || T->isFunctionType());
3053+
3054+
// The unbraced `extern "C"` invariant is that the storage class
3055+
// specifier is omitted in the source code, i.e. SC_None (but is,
3056+
// implicitly, `extern`).
3057+
assert(!isUnbracedLanguageLinkage(DC) || S == SC_None);
3058+
30443059
FunctionDeclBits.SClass = S;
30453060
FunctionDeclBits.IsInline = isInlineSpecified;
30463061
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)