On 05.03.2014 19:43, Richard Smith wrote:
> If we don't adjust the DeclContext, I don't think we'll reject ill-formed
> code such as this:
>
> float x;
> void f() {
> __declspec(dllimport) int x;
> }
This test fails with the original patch, but this problem can be solved
by setting the storage class a bit earlier even without setting the
correct DeclContext. I have yet to come up with a test case that exposes
the wrong DeclContext.
I've restructured the patch to look at the parsed attributes to get the
correct DeclContext. Another option I explored was using setDeclContext
on the VarDecl. This passes all tests but I'm more reluctant to do this.
-Nico
>From 52fe4cb18a4d2a6c2c8cbdf2fff512a8e8717fe4 Mon Sep 17 00:00:00 2001
From: Nico Rieck <[email protected]>
Date: Mon, 20 Jan 2014 19:28:12 +0100
Subject: [PATCH] dll-sema-global-dllimport-extern
Treat dllimport globals without explicit storage class as extern
dllimport implies a definition which means the 'extern' keyword is optional
when declaring imported variables.
---
lib/Sema/SemaDecl.cpp | 35 +++++++++++++++++++++++++++++++++++
test/Sema/dllimport.c | 8 ++++++++
test/SemaCXX/dllimport.cpp | 8 ++++++++
3 files changed, 51 insertions(+)
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 2e8392f..f8badc7 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -4923,6 +4923,27 @@ static bool shouldConsiderLinkage(const FunctionDecl *FD) {
llvm_unreachable("Unexpected context");
}
+static bool hasParsedAttr(Scope *S, const AttributeList *AttrList,
+ AttributeList::Kind Kind) {
+ for (const AttributeList* L = AttrList; L; L = L->getNext())
+ if (L->getKind() == Kind)
+ return true;
+ return false;
+}
+
+static bool hasParsedAttr(Scope *S, const Declarator &PD,
+ AttributeList::Kind Kind) {
+ if (hasParsedAttr(S, PD.getDeclSpec().getAttributes().getList(), Kind))
+ return true;
+
+ for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) {
+ if (hasParsedAttr(S, PD.getTypeObject(i).getAttrs(), Kind))
+ return true;
+ }
+
+ return hasParsedAttr(S, PD.getAttributes(), Kind);
+}
+
/// Adjust the \c DeclContext for a function or variable that might be a
/// function-local external declaration.
bool Sema::adjustContextForLocalExternDecl(DeclContext *&DC) {
@@ -4959,6 +4980,12 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,
VarDecl::StorageClass SC =
StorageClassSpecToVarDeclStorageClass(D.getDeclSpec());
+ // dllimport globals without explicit storage class are treated as extern. We
+ // have to change the storage class this early to get the right DeclContext.
+ if (SC == SC_None && !DC->isRecord() &&
+ hasParsedAttr(S, D, AttributeList::AT_DLLImport))
+ SC = SC_Extern;
+
DeclContext *OriginalDC = DC;
bool IsLocalExternDecl = SC == SC_Extern &&
adjustContextForLocalExternDecl(DC);
@@ -5326,6 +5353,14 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,
}
}
+ // Ensure that dllimport globals without explicit storage class are treated as
+ // extern. The storage class is set above using parsed attributes. Now we can
+ // check the VarDecl itself.
+ if (const DLLImportAttr *DA = NewVD->getAttr<DLLImportAttr>()) {
+ assert(DA->isInherited() || NewVD->isStaticDataMember() ||
+ NewVD->getStorageClass() != SC_None);
+ }
+
// In auto-retain/release, infer strong retension for variables of
// retainable type.
if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(NewVD))
diff --git a/test/Sema/dllimport.c b/test/Sema/dllimport.c
index 615f8f7..683c732 100644
--- a/test/Sema/dllimport.c
+++ b/test/Sema/dllimport.c
@@ -32,12 +32,20 @@ int __declspec(dllimport) GlobalInit2 = 1; // expected-error{{definition of dlli
__declspec(dllimport) extern int ExternGlobalDeclInit;
int ExternGlobalDeclInit = 1; // expected-error{{definition of dllimport data}}
+__declspec(dllimport) int GlobalDeclInit;
+int GlobalDeclInit = 1; // expected-error{{definition of dllimport data}}
+
// Redeclarations
__declspec(dllimport) extern int GlobalRedecl1;
__declspec(dllimport) extern int GlobalRedecl1;
+__declspec(dllimport) int GlobalRedecl2;
+__declspec(dllimport) int GlobalRedecl2;
+
// Import in local scope.
+__declspec(dllimport) float LocalRedecl; // expected-note{{previous definition is here}}
void functionScope() {
+ __declspec(dllimport) int LocalRedecl; // expected-error{{redefinition of 'LocalRedecl' with a different type: 'int' vs 'float'}}
__declspec(dllimport) int LocalVarDecl;
__declspec(dllimport) int LocalVarDef = 1; // expected-error{{definition of dllimport data}}
__declspec(dllimport) extern int ExternLocalVarDecl;
diff --git a/test/SemaCXX/dllimport.cpp b/test/SemaCXX/dllimport.cpp
index 4a3ee20..a9c58fd 100644
--- a/test/SemaCXX/dllimport.cpp
+++ b/test/SemaCXX/dllimport.cpp
@@ -44,12 +44,20 @@ int __declspec(dllimport) GlobalInit2 = 1; // expected-error{{definition of dlli
__declspec(dllimport) extern int ExternGlobalDeclInit;
int ExternGlobalDeclInit = 1; // expected-error{{definition of dllimport data}}
+__declspec(dllimport) int GlobalDeclInit;
+int GlobalDeclInit = 1; // expected-error{{definition of dllimport data}}
+
// Redeclarations
__declspec(dllimport) extern int GlobalRedecl1;
__declspec(dllimport) extern int GlobalRedecl1;
+__declspec(dllimport) int GlobalRedecl2;
+__declspec(dllimport) int GlobalRedecl2;
+
// Import in local scope.
+__declspec(dllimport) float LocalRedecl; // expected-note{{previous definition is here}}
void functionScope() {
+ __declspec(dllimport) int LocalRedecl; // expected-error{{redefinition of 'LocalRedecl' with a different type: 'int' vs 'float'}}
__declspec(dllimport) int LocalVarDecl;
__declspec(dllimport) int LocalVarDef = 1; // expected-error{{definition of dllimport data}}
__declspec(dllimport) extern int ExternLocalVarDecl;
--
1.9.0.msysgit.0
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits