Skip to content

Commit 2745f7d

Browse files
author
Mandeep Singh Grang
authored
Small improvements in bounds widening code (#877)
- Removed double semicolons - Replaced std::sort with llvm::sort - Added IgnoreParens in SplitIntoBaseOffset function - Added an llvm::raw_ostream object to the BoundsAnalysis class - Added a unit test for parenthesized bounds declaration and dereference
1 parent e943385 commit 2745f7d

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

clang/include/clang/Sema/BoundsAnalysis.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ namespace clang {
105105
CFG *Cfg;
106106
ASTContext &Ctx;
107107
Lexicographic Lex;
108+
llvm::raw_ostream &OS;
108109

109110
class ElevatedCFGBlock {
110111
public:
@@ -183,7 +184,7 @@ namespace clang {
183184
public:
184185
BoundsAnalysis(Sema &S, CFG *Cfg) :
185186
S(S), Cfg(Cfg), Ctx(S.Context),
186-
Lex(Lexicographic(Ctx, nullptr)) {}
187+
Lex(Lexicographic(Ctx, nullptr)), OS(llvm::outs()) {}
187188

188189
// Run the dataflow analysis to widen bounds for ntptr's.
189190
// @param[in] FD is the current function.

clang/lib/Sema/BoundsAnalysis.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,20 @@ ExprIntPairTy BoundsAnalysis::SplitIntoBaseOffset(const Expr *E) {
423423
llvm::APSInt Zero (Ctx.getTypeSize(Ctx.IntTy), 0);
424424

425425
if (!E)
426-
return std::make_pair(nullptr, Zero);;
426+
return std::make_pair(nullptr, Zero);
427+
428+
E = E->IgnoreParens();
427429

428430
if (IsDeclOperand(E))
429431
return std::make_pair(GetDeclOperand(E), Zero);
430432

431433
if (!isa<BinaryOperator>(E))
432-
return std::make_pair(nullptr, Zero);;
434+
return std::make_pair(nullptr, Zero);
433435

434436
const BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
435437
// TODO: Currently we only handle exprs containing additive operations.
436438
if (BO->getOpcode() != BO_Add)
437-
return std::make_pair(nullptr, Zero);;
439+
return std::make_pair(nullptr, Zero);
438440

439441
Expr *LHS = BO->getLHS()->IgnoreParens();
440442
Expr *RHS = BO->getRHS()->IgnoreParens();
@@ -472,7 +474,7 @@ ExprIntPairTy BoundsAnalysis::SplitIntoBaseOffset(const Expr *E) {
472474
// was a BinaryOperator, or because the RHS was a BinaryOperator and was
473475
// swapped with the LHS.
474476
if (!isa<BinaryOperator>(LHS))
475-
return std::make_pair(nullptr, Zero);;
477+
return std::make_pair(nullptr, Zero);
476478

477479
// If we reach here, the expr is one of these:
478480
// Case 4: (p + q) + i
@@ -878,25 +880,26 @@ OrderedBlocksTy BoundsAnalysis::GetOrderedBlocks() {
878880
}
879881

880882
void BoundsAnalysis::DumpWidenedBounds(FunctionDecl *FD) {
881-
llvm::outs() << "--------------------------------------\n";
882-
llvm::outs() << "In function: " << FD->getName() << "\n";
883+
OS << "--------------------------------------\n";
884+
OS << "In function: " << FD->getName() << "\n";
883885

884886
for (const CFGBlock *B : GetOrderedBlocks()) {
885-
llvm::outs() << "--------------------------------------";
886-
B->print(llvm::outs(), Cfg, S.getLangOpts(), /* ShowColors */ true);
887+
OS << "--------------------------------------";
888+
B->print(OS, Cfg, S.getLangOpts(), /* ShowColors */ true);
887889

888890
BoundsMapTy Vars = GetWidenedBounds(B);
889891
using VarPairTy = std::pair<const VarDecl *, unsigned>;
890892

891-
std::sort(Vars.begin(), Vars.end(), [](VarPairTy A, VarPairTy B) {
892-
return A.first->getQualifiedNameAsString().compare(
893-
B.first->getQualifiedNameAsString()) < 0;
894-
});
893+
llvm::sort(Vars.begin(), Vars.end(),
894+
[](VarPairTy A, VarPairTy B) {
895+
return A.first->getQualifiedNameAsString().compare(
896+
B.first->getQualifiedNameAsString()) < 0;
897+
});
895898

896899
for (auto item : Vars) {
897-
llvm::outs() << "upper_bound("
898-
<< item.first->getQualifiedNameAsString() << ") = "
899-
<< item.second << "\n";
900+
OS << "upper_bound("
901+
<< item.first->getQualifiedNameAsString() << ") = "
902+
<< item.second << "\n";
900903
}
901904
}
902905
}

clang/test/CheckedC/inferred-bounds/widened-bounds.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,3 +1230,15 @@ void f32() {
12301230
// CHECK: case 0:
12311231
// CHECK-NOT: upper_bound(p)
12321232
}
1233+
1234+
void f33() {
1235+
_Nt_array_ptr<char> p : bounds(p, ((((((p + 1))))))) = "a";
1236+
1237+
if (*(((p + 1)))) {}
1238+
1239+
// CHECK: In function: f33
1240+
// CHECK: [B2]
1241+
// CHECK: 2: *(((p + 1)))
1242+
// CHECK: [B1]
1243+
// CHECK: upper_bound(p) = 1
1244+
}

0 commit comments

Comments
 (0)