Skip to content

Commit a6f21e1

Browse files
authored
Merge pull request #277 from plum-umd/iss276
Fixing issue #276
2 parents 49b1ae1 + 6b96bdd commit a6f21e1

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

clang/include/clang/CConv/ConstraintVariables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ class PointerVariableConstraint : public ConstraintVariable {
297297
bool getArrPresent() const { return ArrPresent; }
298298
// Check if the outermost pointer is an unsized array.
299299
bool isTopCvarUnsizedArr() const;
300+
// Check if any of the pointers is either a sized or unsized arr.
301+
bool hasSomeSizedArr() const;
300302

301303
// Is an itype present for this constraint? If yes,
302304
// what is the text of that itype?

clang/lib/CConv/AVarBoundsInfo.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,15 +692,14 @@ bool AvarBoundsInference::inferPossibleBounds(BoundsKey K, ABounds *SB,
692692
auto *SBVar = BI->getProgramVar(SBKey);
693693
if (SBVar->IsNumConstant()) {
694694
PotentialB.insert(SBVar);
695-
} else {
696-
// Find all the in scope variables reachable from the current
697-
// bounds variable.
698-
ScopeVisitor TV(Kvar->getScope(), PotentialB, BI->PVarInfo,
699-
BI->PointerBoundsKey);
700-
BKGraph.visitBreadthFirst(SBKey, [&TV](BoundsKey BK) {
701-
TV.visitBoundsKey(BK);
702-
});
703695
}
696+
// Find all the in scope variables reachable from the current
697+
// bounds variable.
698+
ScopeVisitor TV(Kvar->getScope(), PotentialB, BI->PVarInfo,
699+
BI->PointerBoundsKey);
700+
BKGraph.visitBreadthFirst(SBKey, [&TV](BoundsKey BK) {
701+
TV.visitBoundsKey(BK);
702+
});
704703

705704
if (*Kvar->getScope() == *SBVar->getScope()) {
706705
PotentialB.insert(SBVar);

clang/lib/CConv/ConstraintVariables.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ PointerVariableConstraint::PointerVariableConstraint(const QualType &QT,
151151
ArrPresent = false;
152152

153153
bool IsDeclTy = false;
154+
155+
auto &ABInfo = I.getABoundsInfo();
154156
if (D != nullptr) {
155-
auto &ABInfo = I.getABoundsInfo();
156157
if (ABInfo.tryGetVariable(D, BKey)) {
157158
ValidBoundsKey = true;
158159
}
@@ -210,6 +211,7 @@ PointerVariableConstraint::PointerVariableConstraint(const QualType &QT,
210211
bool VarCreated = false;
211212
bool IsArr = false;
212213
bool IsIncompleteArr = false;
214+
bool IsTopMost = true;
213215
OriginallyChecked = false;
214216
uint32_t TypeIdx = 0;
215217
std::string Npre = inFunc ? ((*inFunc)+":") : "";
@@ -268,7 +270,14 @@ PointerVariableConstraint::PointerVariableConstraint(const QualType &QT,
268270
// See if there is a constant size to this array type at this position.
269271
if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
270272
arrSizes[TypeIdx] = std::pair<OriginalArrType,uint64_t>(
271-
O_SizedArray,CAT->getSize().getZExtValue());
273+
O_SizedArray, CAT->getSize().getZExtValue());
274+
275+
// If this is the top-most pointer variable?
276+
if (hasBoundsKey() && IsTopMost) {
277+
BoundsKey CBKey = ABInfo.getConstKey(CAT->getSize().getZExtValue());
278+
ABounds *NB = new CountBound(CBKey);
279+
ABInfo.insertDeclaredBounds(D, NB);
280+
}
272281
} else {
273282
arrSizes[TypeIdx] = std::pair<OriginalArrType,uint64_t>(
274283
O_UnSizedArray,0);
@@ -316,6 +325,7 @@ PointerVariableConstraint::PointerVariableConstraint(const QualType &QT,
316325
TypeIdx++;
317326
Npre = Npre + "*";
318327
VK = VarAtom::V_Other; // only the outermost pointer considered a param/return
328+
IsTopMost = false;
319329
}
320330
insertQualType(TypeIdx, QTy);
321331

@@ -1148,6 +1158,16 @@ bool PointerVariableConstraint::isTopCvarUnsizedArr() const {
11481158
return true;
11491159
}
11501160

1161+
bool PointerVariableConstraint::hasSomeSizedArr() const {
1162+
for (auto &AS : arrSizes) {
1163+
if (AS.second.first == O_SizedArray ||
1164+
AS.second.second == O_UnSizedArray) {
1165+
return true;
1166+
}
1167+
}
1168+
return false;
1169+
}
1170+
11511171
bool PointerVariableConstraint::
11521172
solutionEqualTo(Constraints &CS, const ConstraintVariable *CV) const {
11531173
bool Ret = false;

clang/lib/CConv/RewriteUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ std::string ArrayBoundsRewriter::getBoundsString(PVConstraint *PV,
342342
// For itype we do not want to add a second ":".
343343
std::string Pfix = Isitype ? " " : " : ";
344344

345-
if (ValidBKey) {
345+
if (ValidBKey && !PV->hasSomeSizedArr()) {
346346
ABounds *ArrB = ABInfo.getBounds(DK);
347347
// Only we we have bounds and no pointer arithmetic on the variable.
348348
if (ArrB != nullptr && !ABInfo.hasPointerArithmetic(DK)) {

clang/test/CheckedCRewriter/basic_checks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void typd_driver(void) {
2727

2828
//CHECK_ALL: wchar_t buf _Checked[10];
2929
//CHECK_ALL: _Ptr<wchar_t> a = &buf[0];
30-
//CHECK_ALL: _Array_ptr<wchar_t> b = &buf[0];
30+
//CHECK_ALL: _Array_ptr<wchar_t> b : count(10) = &buf[0];
3131

3232

3333
typedef struct _A {

clang/test/CheckedCRewriter/simple_locals.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void pullit(char *base, char *out, int *index) {
151151

152152
return;
153153
}
154-
//CHECK_ALL: void pullit(_Array_ptr<char> base, _Ptr<char> out, _Ptr<int> index) _Checked {
154+
//CHECK_ALL: void pullit(_Array_ptr<char> base : count(10), _Ptr<char> out, _Ptr<int> index) _Checked {
155155
//CHECK_NOALL: void pullit(char *base, _Ptr<char> out, _Ptr<int> index) {
156156

157157
void driver() {

0 commit comments

Comments
 (0)