Skip to content

Commit 1bca902

Browse files
committed
Fixed the addition of storage qualifiers (i.e., static and extern)
1 parent b06f1b1 commit 1bca902

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

tools/checked-c-convert/RewriteUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ bool CastPlacementVisitor::VisitFunctionDecl(FunctionDecl *FD) {
522522
}
523523
}
524524

525-
s = returnVar + cDecl->getName() + "(";
525+
s = getStorageQualifierString(Definition) + returnVar + cDecl->getName() + "(";
526526
if (parmStrs.size() > 0) {
527527
std::ostringstream ss;
528528

@@ -809,8 +809,8 @@ void RewriteConsumer::HandleTranslationUnit(ASTContext &Context) {
809809

810810
if (PV && PV->anyChanges(Info.getConstraints().getVariables())) {
811811
// Rewrite a declaration.
812-
std::string newTy = PV->mkString(Info.getConstraints().getVariables());
813-
rewriteThese.insert(DAndReplace(D, DS, newTy));;
812+
std::string newTy = getStorageQualifierString(D) + PV->mkString(Info.getConstraints().getVariables());
813+
rewriteThese.insert(DAndReplace(D, DS, newTy));
814814
} else if (FV && FV->anyChanges(Info.getConstraints().getVariables())) {
815815
// Rewrite a function variables return value.
816816
std::set<ConstraintVariable*> V = FV->getReturnVars();

tools/checked-c-convert/RewriteUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ struct DAndReplace
3333
Statement(nullptr),
3434
Replacement(""),
3535
fullDecl(false) { }
36+
DAndReplace(const DAndReplace &other, std::string R): Declaration(other.Declaration),
37+
Statement(other.Statement),
38+
Replacement(R),
39+
fullDecl(other.fullDecl) {}
40+
3641

3742
DAndReplace(Decl *D, std::string R) : Declaration(D),
3843
Statement(nullptr),

tools/checked-c-convert/Utils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,25 @@ bool isFunctionPointerExpr(clang::Expr *toCheck) {
134134
return dyn_cast<FunctionDecl>(DRE->getDecl()) != nullptr;
135135
}
136136
return false;
137+
}
138+
139+
static std::string storageClassToString(StorageClass SC) {
140+
switch(SC) {
141+
case StorageClass::SC_Static: return "static ";
142+
case StorageClass::SC_Extern: return "extern ";
143+
// no default class, we do not care.
144+
}
145+
return "";
146+
}
147+
148+
// this method gets the storage qualifier for the
149+
// provided declaration i.e., static, extern, etc.
150+
std::string getStorageQualifierString(Decl *D) {
151+
if(FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
152+
return storageClassToString(FD->getStorageClass());
153+
}
154+
if(VarDecl *VD = dyn_cast<VarDecl>(D)) {
155+
return storageClassToString(VD->getStorageClass());
156+
}
157+
return "";
137158
}

tools/checked-c-convert/Utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ bool hasFunctionBody(clang::Decl *param);
4040

4141
bool isFunctionPointerExpr(clang::Expr *toCheck);
4242

43+
std::string getStorageQualifierString(clang::Decl *D);
44+
4345
clang::SourceLocation getFunctionDeclarationEnd(clang::FunctionDecl *FD, clang::SourceManager &S);
4446
#endif

0 commit comments

Comments
 (0)