Skip to content

Commit 6ba783c

Browse files
committed
Add error checking
1 parent b54bd17 commit 6ba783c

File tree

6 files changed

+83
-19
lines changed

6 files changed

+83
-19
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9498,6 +9498,8 @@ def warn_redefine_extname_not_applied : Warning<
94989498
"#pragma redefine_extname is applicable to external C declarations only; "
94999499
"not applied to %select{function|variable}0 %1">,
95009500
InGroup<Pragmas>;
9501+
def err_cannot_be_exported : Error<
9502+
"cannot be '_Export` qualified">;
95019503
} // End of general sema category.
95029504

95039505
// inline asm.

clang/lib/Parse/ParseStmt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
538538
case tok::annot_pragma_attribute:
539539
HandlePragmaAttribute();
540540
return StmtEmpty();
541+
541542
case tok::annot_pragma_export:
542543
ProhibitAttributes(CXX11Attrs);
543544
ProhibitAttributes(GNUAttrs);

clang/lib/Sema/SemaDecl.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6503,10 +6503,10 @@ NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D,
65036503
existingAttr->getVisibility();
65046504
if (existingValue != VisibilityAttr::Default)
65056505
Diag(D.getExportLoc(), diag::err_mismatched_visibility);
6506-
} else {
6506+
} else
6507+
// Add VisibilityAttr::Default since the default could be hidden, etc
65076508
New->addAttr(
65086509
VisibilityAttr::CreateImplicit(Context, VisibilityAttr::Default));
6509-
}
65106510
}
65116511

65126512
// If this has an identifier and is not a function template specialization,
@@ -6746,6 +6746,9 @@ Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC,
67466746
return nullptr;
67476747
}
67486748

6749+
if (D.IsExport())
6750+
Diag(D.getName().StartLocation, diag::err_cannot_be_exported);
6751+
67496752
TypedefDecl *NewTD = ParseTypedefDecl(S, D, TInfo->getType(), TInfo);
67506753
if (!NewTD) return nullptr;
67516754

@@ -8208,6 +8211,9 @@ NamedDecl *Sema::ActOnVariableDeclarator(
82088211

82098212
ProcessPragmaWeak(S, NewVD);
82108213

8214+
if (D.IsExport() && !NewVD->hasExternalFormalLinkage())
8215+
Diag(D.getIdentifierLoc(), diag::err_cannot_be_exported);
8216+
82118217
// If this is the first declaration of an extern C variable, update
82128218
// the map of such variables.
82138219
if (NewVD->isFirstDecl() && !NewVD->isInvalidDecl() &&
@@ -10851,6 +10857,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
1085110857
}
1085210858

1085310859
ProcessPragmaWeak(S, NewFD);
10860+
10861+
if (D.IsExport() && !NewFD->hasExternalFormalLinkage())
10862+
Diag(D.getIdentifierLoc(), diag::err_cannot_be_exported);
10863+
1085410864
checkAttributesAfterMerging(*this, *NewFD);
1085510865

1085610866
AddKnownFunctionAttributes(NewFD);
@@ -15343,6 +15353,9 @@ Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D,
1534315353
if (getLangOpts().OpenCL)
1534415354
deduceOpenCLAddressSpace(New);
1534515355

15356+
if (D.IsExport())
15357+
Diag(D.getIdentifierLoc(), diag::err_cannot_be_exported);
15358+
1534615359
return New;
1534715360
}
1534815361

@@ -18705,6 +18718,9 @@ FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record,
1870518718
} else
1870618719
Record->addDecl(NewFD);
1870718720

18721+
if (D.IsExport())
18722+
Diag(D.getIdentifierLoc(), diag::err_cannot_be_exported);
18723+
1870818724
return NewFD;
1870918725
}
1871018726

clang/test/Sema/attr-export-failing.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,3 @@ int v0;
1313
static int s0; // expected-warning{{#pragma export is applicable to symbols with external linkage only; not applied to 's0'}}
1414
#pragma export(sf0)
1515
#pragma export(s0)
16-
17-
typedef int _Export ty;
18-
ty x;
19-
int f(int _Export x);
20-
static int _Export s;
21-
struct S {
22-
int _Export nonstaticdatamember;
23-
};
24-
void g() {
25-
int _Export automatic;
26-
}
27-

clang/test/Sema/zos-export.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
// RUN: %clang_cc1 -triple s390x-ibm-zos %s -fsyntax-only -verify
22

3-
typedef int _Export ty;
3+
typedef int _Export ty; //expected-error {{cannot be '_Export` qualified}}
44
ty x;
5-
int f(int _Export x);
6-
static int _Export s;
5+
int f(int _Export argument); //expected-error {{cannot be '_Export` qualified}}
6+
static int _Export file_scope_static; //expected-error {{cannot be '_Export` qualified}}
77
struct S {
8-
int _Export nonstaticdatamember;
8+
int _Export nonstaticdatamember; //expected-error {{cannot be '_Export` qualified}}
99
};
1010
void g() {
11-
int _Export automatic;
11+
int _Export automatic; //expected-error {{cannot be '_Export` qualified}}
1212
}
13+
14+
static void _Export static_func() { //expected-error {{cannot be '_Export` qualified}}
15+
}
16+
17+
void _Export h() {
18+
static_func();
19+
}
20+
21+
void j() {
22+
static int _Export sl = 0; //expected-error {{cannot be '_Export` qualified}}
23+
}
24+
25+
int _Export file_scope;

clang/test/Sema/zos-export.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %clang_cc1 -triple s390x-ibm-zos %s -fsyntax-only -verify
2+
3+
typedef int _Export ty; //expected-error {{cannot be '_Export` qualified}}
4+
ty typedef_var;
5+
int f(int _Export argument); //expected-error {{cannot be '_Export` qualified}}
6+
static int _Export file_scope_static; //expected-error {{cannot be '_Export` qualified}}
7+
struct S {
8+
int _Export nonstaticdatamember; //expected-error {{cannot be '_Export` qualified}}
9+
};
10+
void g() {
11+
int _Export automatic; //expected-error {{cannot be '_Export` qualified}}
12+
}
13+
14+
static void _Export static_func() { //expected-error {{cannot be '_Export` qualified}}
15+
}
16+
17+
void _Export h() {
18+
static_func();
19+
}
20+
21+
void j() {
22+
static int _Export sl = 0; //expected-error {{cannot be '_Export` qualified}}
23+
}
24+
25+
int _Export file_scope;
26+
27+
struct _Export SE {
28+
};
29+
30+
struct ST {
31+
void _Export f();
32+
virtual void _Export v_();
33+
static int _Export i;
34+
};
35+
36+
namespace {
37+
int _Export anon_var; //expected-error {{cannot be '_Export` qualified}}
38+
extern "C" int _Export anon_C_var;
39+
void _Export anon_f() {} //expected-error {{cannot be '_Export` qualified}}
40+
extern "C" void _Export anon_C_f() {}
41+
struct anon_S {
42+
static int _Export anon_static_data_member; //expected-error {{cannot be '_Export` qualified}}
43+
};
44+
}

0 commit comments

Comments
 (0)