Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/CConv/ConstraintVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ class PointerVariableConstraint : public ConstraintVariable {
bool getArrPresent() const { return ArrPresent; }
// Check if the outermost pointer is an unsized array.
bool isTopCvarUnsizedArr() const;
// Check if any of the pointers is either a sized or unsized arr.
bool hasSomeSizedArr() const;

// Is an itype present for this constraint? If yes,
// what is the text of that itype?
Expand Down
15 changes: 7 additions & 8 deletions clang/lib/CConv/AVarBoundsInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,14 @@ bool AvarBoundsInference::inferPossibleBounds(BoundsKey K, ABounds *SB,
auto *SBVar = BI->getProgramVar(SBKey);
if (SBVar->IsNumConstant()) {
PotentialB.insert(SBVar);
} else {
// Find all the in scope variables reachable from the current
// bounds variable.
ScopeVisitor TV(Kvar->getScope(), PotentialB, BI->PVarInfo,
BI->PointerBoundsKey);
BKGraph.visitBreadthFirst(SBKey, [&TV](BoundsKey BK) {
TV.visitBoundsKey(BK);
});
}
// Find all the in scope variables reachable from the current
// bounds variable.
ScopeVisitor TV(Kvar->getScope(), PotentialB, BI->PVarInfo,
BI->PointerBoundsKey);
BKGraph.visitBreadthFirst(SBKey, [&TV](BoundsKey BK) {
TV.visitBoundsKey(BK);
});

if (*Kvar->getScope() == *SBVar->getScope()) {
PotentialB.insert(SBVar);
Expand Down
24 changes: 22 additions & 2 deletions clang/lib/CConv/ConstraintVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ PointerVariableConstraint::PointerVariableConstraint(const QualType &QT,
ArrPresent = false;

bool IsDeclTy = false;

auto &ABInfo = I.getABoundsInfo();
if (D != nullptr) {
auto &ABInfo = I.getABoundsInfo();
if (ABInfo.tryGetVariable(D, BKey)) {
ValidBoundsKey = true;
}
Expand Down Expand Up @@ -210,6 +211,7 @@ PointerVariableConstraint::PointerVariableConstraint(const QualType &QT,
bool VarCreated = false;
bool IsArr = false;
bool IsIncompleteArr = false;
bool IsTopMost = true;
OriginallyChecked = false;
uint32_t TypeIdx = 0;
std::string Npre = inFunc ? ((*inFunc)+":") : "";
Expand Down Expand Up @@ -268,7 +270,14 @@ PointerVariableConstraint::PointerVariableConstraint(const QualType &QT,
// See if there is a constant size to this array type at this position.
if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
arrSizes[TypeIdx] = std::pair<OriginalArrType,uint64_t>(
O_SizedArray,CAT->getSize().getZExtValue());
O_SizedArray, CAT->getSize().getZExtValue());

// If this is the top-most pointer variable?
if (hasBoundsKey() && IsTopMost) {
BoundsKey CBKey = ABInfo.getConstKey(CAT->getSize().getZExtValue());
ABounds *NB = new CountBound(CBKey);
ABInfo.insertDeclaredBounds(D, NB);
}
} else {
arrSizes[TypeIdx] = std::pair<OriginalArrType,uint64_t>(
O_UnSizedArray,0);
Expand Down Expand Up @@ -316,6 +325,7 @@ PointerVariableConstraint::PointerVariableConstraint(const QualType &QT,
TypeIdx++;
Npre = Npre + "*";
VK = VarAtom::V_Other; // only the outermost pointer considered a param/return
IsTopMost = false;
}
insertQualType(TypeIdx, QTy);

Expand Down Expand Up @@ -1148,6 +1158,16 @@ bool PointerVariableConstraint::isTopCvarUnsizedArr() const {
return true;
}

bool PointerVariableConstraint::hasSomeSizedArr() const {
for (auto &AS : arrSizes) {
if (AS.second.first == O_SizedArray ||
AS.second.second == O_UnSizedArray) {
return true;
}
}
return false;
}

bool PointerVariableConstraint::
solutionEqualTo(Constraints &CS, const ConstraintVariable *CV) const {
bool Ret = false;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CConv/RewriteUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ std::string ArrayBoundsRewriter::getBoundsString(PVConstraint *PV,
// For itype we do not want to add a second ":".
std::string Pfix = Isitype ? " " : " : ";

if (ValidBKey) {
if (ValidBKey && !PV->hasSomeSizedArr()) {
ABounds *ArrB = ABInfo.getBounds(DK);
// Only we we have bounds and no pointer arithmetic on the variable.
if (ArrB != nullptr && !ABInfo.hasPointerArithmetic(DK)) {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CheckedCRewriter/basic_checks.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void typd_driver(void) {

//CHECK_ALL: wchar_t buf _Checked[10];
//CHECK_ALL: _Ptr<wchar_t> a = &buf[0];
//CHECK_ALL: _Array_ptr<wchar_t> b = &buf[0];
//CHECK_ALL: _Array_ptr<wchar_t> b : count(10) = &buf[0];


typedef struct _A {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CheckedCRewriter/simple_locals.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void pullit(char *base, char *out, int *index) {

return;
}
//CHECK_ALL: void pullit(_Array_ptr<char> base, _Ptr<char> out, _Ptr<int> index) _Checked {
//CHECK_ALL: void pullit(_Array_ptr<char> base : count(10), _Ptr<char> out, _Ptr<int> index) _Checked {
//CHECK_NOALL: void pullit(char *base, _Ptr<char> out, _Ptr<int> index) {

void driver() {
Expand Down