Skip to content

Commit 8ea9f3b

Browse files
authored
Merge pull request #331 from kyleheadley/bugfix#301
Add extern and static qualifiers to initialized variables
2 parents 0086cf1 + 43d2e10 commit 8ea9f3b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

clang/lib/3C/StructInit.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using namespace clang;
1818

1919
bool StructVariableInitializer::variableNeedsInitializer(VarDecl *VD) {
20+
if (VD->getStorageClass() == StorageClass::SC_Extern)
21+
return false;
2022
RecordDecl *RD = VD->getType().getTypePtr()->getAsRecordDecl();
2123
if (RecordDecl *Definition = RD->getDefinition()) {
2224
// See if we already know that this structure has a checked pointer.
@@ -49,7 +51,11 @@ void StructVariableInitializer::insertVarDecl(VarDecl *VD, DeclStmt *S) {
4951
if (variableNeedsInitializer(VD)) {
5052
// Create replacement declaration text with an initializer.
5153
const clang::Type *Ty = VD->getType().getTypePtr();
52-
std::string ToReplace = tyToStr(Ty) + " " + VD->getName().str() + " = {}";
54+
std::string TQ = VD->getType().getQualifiers().getAsString();
55+
if (!TQ.empty()) { TQ += " "; }
56+
std::string ToReplace =
57+
getStorageQualifierString(VD) + TQ +
58+
tyToStr(Ty) + " " + VD->getName().str() + " = {}";
5359
RewriteThese.insert(new VarDeclReplacement(VD, S, ToReplace));
5460
}
5561
}

clang/test/3C/qualifiers.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,17 @@ void mixed() {
5252
}
5353
//CHECK: const volatile restrict _Ptr<int> a = ((void *)0);
5454
//CHECK: restrict _Ptr<const volatile _Ptr<const _Ptr<const volatile int>>> b = ((void *)0);
55+
56+
struct qualifier_struct { int *a; };
57+
void structs() {
58+
struct qualifier_struct a0;
59+
static struct qualifier_struct a;
60+
volatile static struct qualifier_struct b;
61+
const static struct qualifier_struct *c;
62+
const extern struct qualifier_struct d;
63+
}
64+
//CHECK: struct qualifier_struct a0 = {};
65+
//CHECK: static struct qualifier_struct a = {};
66+
//CHECK: static volatile struct qualifier_struct b = {};
67+
//CHECK: static _Ptr<const struct qualifier_struct> c = ((void *)0);
68+
//CHECK: const extern struct qualifier_struct d;

0 commit comments

Comments
 (0)