diff options
author | Igor Kudrin <ikudrin@accesssoftek.com> | 2025-07-14 15:44:43 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-14 15:44:43 -0700 |
commit | 00dacf8c22f065cb52efb14cd091d441f19b319e (patch) | |
tree | 4f08dcec67d6cd6b89a7cd3602142e674702232c /clang/lib/Analysis/UninitializedValues.cpp | |
parent | 244ebef1ddbc6a77f17c36562d4a4292654940a6 (diff) | |
download | llvm-00dacf8c22f065cb52efb14cd091d441f19b319e.zip llvm-00dacf8c22f065cb52efb14cd091d441f19b319e.tar.gz llvm-00dacf8c22f065cb52efb14cd091d441f19b319e.tar.bz2 |
[clang] Add -Wuninitialized-const-pointer (#148337)
This option is similar to -Wuninitialized-const-reference, but diagnoses
the passing of an uninitialized value via a const pointer, like in the
following code:
```
void foo(const int *);
void test() {
int v;
foo(&v);
}
```
This is an extract from #147221 as suggested in [this
comment](https://github.com/llvm/llvm-project/pull/147221#discussion_r2190998730).
Diffstat (limited to 'clang/lib/Analysis/UninitializedValues.cpp')
-rw-r--r-- | clang/lib/Analysis/UninitializedValues.cpp | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/clang/lib/Analysis/UninitializedValues.cpp b/clang/lib/Analysis/UninitializedValues.cpp index 8c9cf8d..0175d4a4 100644 --- a/clang/lib/Analysis/UninitializedValues.cpp +++ b/clang/lib/Analysis/UninitializedValues.cpp @@ -276,13 +276,7 @@ namespace { /// escaped the analysis and will be treated as an initialization. class ClassifyRefs : public StmtVisitor<ClassifyRefs> { public: - enum Class { - Init, - Use, - SelfInit, - ConstRefUse, - Ignore - }; + enum Class { Init, Use, SelfInit, ConstRefUse, ConstPtrUse, Ignore }; private: const DeclContext *DC; @@ -451,8 +445,9 @@ void ClassifyRefs::VisitCallExpr(CallExpr *CE) { const Expr *Ex = stripCasts(DC->getParentASTContext(), *I); const auto *UO = dyn_cast<UnaryOperator>(Ex); if (UO && UO->getOpcode() == UO_AddrOf) - Ex = UO->getSubExpr(); - classify(Ex, Ignore); + classify(UO->getSubExpr(), isTrivialBody ? Ignore : ConstPtrUse); + else + classify(Ex, Ignore); } } } @@ -496,6 +491,7 @@ public: void reportUse(const Expr *ex, const VarDecl *vd); void reportConstRefUse(const Expr *ex, const VarDecl *vd); + void reportConstPtrUse(const Expr *ex, const VarDecl *vd); void VisitBinaryOperator(BinaryOperator *bo); void VisitBlockExpr(BlockExpr *be); @@ -682,6 +678,15 @@ void TransferFunctions::reportConstRefUse(const Expr *ex, const VarDecl *vd) { } } +void TransferFunctions::reportConstPtrUse(const Expr *ex, const VarDecl *vd) { + Value v = vals[vd]; + if (isAlwaysUninit(v)) { + auto use = getUninitUse(ex, vd, v); + use.setConstPtrUse(); + handler.handleUseOfUninitVariable(vd, use); + } +} + void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) { // This represents an initialization of the 'element' value. if (const auto *DS = dyn_cast<DeclStmt>(FS->getElement())) { @@ -754,6 +759,9 @@ void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { case ClassifyRefs::ConstRefUse: reportConstRefUse(dr, cast<VarDecl>(dr->getDecl())); break; + case ClassifyRefs::ConstPtrUse: + reportConstPtrUse(dr, cast<VarDecl>(dr->getDecl())); + break; } } |