diff options
author | Jan Voung <jvoung@google.com> | 2025-03-17 16:04:15 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-17 16:04:15 -0400 |
commit | 6f659b0060d615435ceec53de407a8084656bc98 (patch) | |
tree | dd93f075abf488a0b9634016f9109f14bd9dbc6a /clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp | |
parent | d2e1e3034801da85c495adfffadf1de87f8b4734 (diff) | |
download | llvm-6f659b0060d615435ceec53de407a8084656bc98.zip llvm-6f659b0060d615435ceec53de407a8084656bc98.tar.gz llvm-6f659b0060d615435ceec53de407a8084656bc98.tar.bz2 |
[clang][dataflow] For bugprone-unchecked-optional-access report range (#131055)
Report the range in diagnostics, in addition to the location
in case the range helps disambiguate a little in chained `->`
expressions.
```
b->a->f->x = 1;
^~~~~~~
```
instead of just:
```
b->a->f->x = 1;
^
```
As a followup we should probably also report the location/range
of an `->` if that operator is used. Like:
```
b->a->f->x = 1;
^~
```
Diffstat (limited to 'clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp')
-rw-r--r-- | clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp index c28424f..164d257 100644 --- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp +++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp @@ -1120,8 +1120,8 @@ auto buildTransferMatchSwitch() { .Build(); } -llvm::SmallVector<SourceLocation> diagnoseUnwrapCall(const Expr *ObjectExpr, - const Environment &Env) { +llvm::SmallVector<UncheckedOptionalAccessDiagnostic> +diagnoseUnwrapCall(const Expr *ObjectExpr, const Environment &Env) { if (auto *OptionalLoc = cast_or_null<RecordStorageLocation>( getLocBehindPossiblePointer(*ObjectExpr, Env))) { auto *Prop = Env.getValue(locForHasValue(*OptionalLoc)); @@ -1132,9 +1132,9 @@ llvm::SmallVector<SourceLocation> diagnoseUnwrapCall(const Expr *ObjectExpr, } // Record that this unwrap is *not* provably safe. - // FIXME: include either the name of the optional (if applicable) or a source - // range of the access for easier interpretation of the result. - return {ObjectExpr->getBeginLoc()}; + // FIXME: include the name of the optional (if applicable). + auto Range = CharSourceRange::getTokenRange(ObjectExpr->getSourceRange()); + return {UncheckedOptionalAccessDiagnostic{Range}}; } auto buildDiagnoseMatchSwitch( @@ -1143,8 +1143,9 @@ auto buildDiagnoseMatchSwitch( // lot of duplicated work (e.g. string comparisons), consider providing APIs // that avoid it through memoization. auto IgnorableOptional = ignorableOptional(Options); - return CFGMatchSwitchBuilder<const Environment, - llvm::SmallVector<SourceLocation>>() + return CFGMatchSwitchBuilder< + const Environment, + llvm::SmallVector<UncheckedOptionalAccessDiagnostic>>() // optional::value .CaseOfCFGStmt<CXXMemberCallExpr>( valueCall(IgnorableOptional), |