483 Commits

Author SHA1 Message Date
Jan Voung
66bbbf2e95
[clang][dataflow] Cache accessors returning pointers in bugprone-unchecked-optional-access (#113922)
Previously, we covered returning refs, or copies of optional, and bools.
Now cover returning pointers (to any type).
This is useful for cases like operator-> of smart pointers.
Addresses more of issue llvm#58510
2024-10-28 15:13:29 -04:00
Jan Voung
1f6741c164
[clang][dataflow] Don't clear cached field state if field is const (#113698)
... in the unchecked optional access model.
2024-10-28 10:28:02 -04:00
Florian Mayer
564fd62aed
[FlowSensitive] Allow to dump nested RecordStorageLocation (#112457)
We have an internal analysis that uses them, and the HTML dump would
fail on the assertion.
2024-10-24 11:05:39 -07:00
Thomas Fransham
b8fddca7bd
[llvm] Support llvm::Any across shared libraries on windows (#108051)
This is part of the effort to support for enabling plugins on windows by
adding better support for building llvm as a DLL. The export macros used
here were added in #96630

Since shared library symbols aren't deduplicated across multiple
libraries on windows like Linux we have to manually explicitly import
and export `Any::TypeId` template instantiations for the uses of
`llvm::Any` in the LLVM codebase to support LLVM Windows shared library
builds.
This change ensures that external code, including LLVM's own tests, can
use PassManager callbacks when LLVM is built as a DLL.

I also removed the only use of llvm::Any for LoopNest that only existed
in debug code and there also doesn't seem to be any code creating
`Any<LoopNest>`
2024-10-24 08:07:13 +03:00
Jan Voung
6761b24ae2
[clang][dataflow] Cache accessors for bugprone-unchecked-optional-access (#112605)
Treat calls to zero-param const methods as having stable return values
(with a cache) to address issue #58510. The cache is invalidated when
non-const methods are called. This uses the infrastructure from PR
#111006.

For now we cache methods returning:
- ref to optional
- optional by value
- booleans

We can extend that to pointers to optional in a next change.
2024-10-22 10:18:22 -04:00
Chris Cotter
11c423f9be
[clang-tidy] Add support for bsl::optional (#101450) 2024-09-25 10:54:31 -04:00
Samira Bazuzi
2575ea6e17
[clang][dataflow] Collect local variables referenced within a functio… (#104459)
…n/statement.

We don't need these for the same in-tree purposes as the other sets,
i.e. for making sure we model these Decls that are declared outside the
function, but we have an out-of-tree use for these sets that would
benefit from this simple addition and would avoid duplicating so much of
this code.
2024-08-19 13:25:49 -04:00
Pasquale Riello
28d4149ba0
[clang][dataflow] Fix casting in ChromiumCheckModel. (#101640)
`getDirectCallee()` may return a null pointer if the callee is not a
`FunctionDecl` (for example when using function pointers), this requires
to use `dyn_cast_or_null` instead of `dyn_cast`.
2024-08-02 15:32:47 +02:00
martinboehme
0362a29905
[clang][dataflow] Fix bug in buildContainsExprConsumedInDifferentBlock(). (#100874)
This was missing a call to `ignoreCFGOmittedNodes()`. As a result, the
function
would erroneously conclude that a block did not contain an expression
consumed
in a different block if the expression in question was surrounded by a
`ParenExpr` in the consuming block. The patch adds a test that triggers
this
scenario (and fails without the fix).

To prevent this kind of bug in the future, the patch also adds a new
method
`blockForStmt()` to `AdornedCFG` that calls `ignoreCFGOmittedNodes()`
and is
preferred over accessing `getStmtToBlock()` directly.
2024-07-29 11:24:26 +02:00
Pasquale Riello
49cb170138
[clang][dataflow] Handle CXXInheritedCtorInitExpr in ResultObjectVisitor. (#99616)
`CXXInheritedCtorInitExpr` is another of the node kinds that should be
considered an "original initializer". An assertion failure in
`assert(Children.size() == 1)` happens without this fix.

---------

Co-authored-by: martinboehme <mboehme@google.com>
2024-07-26 15:39:03 +02:00
Samira Bazuzi
83c2bfdacb
[clang][dataflow] Handle this-capturing lambdas in field initializers. (#99519)
We previously would assume these lambdas appeared inside a method
definition and end up crashing.
2024-07-22 10:22:23 -04:00
Samira Bazuzi
3fed312d2b
[clang][dataflow]Propagate the result object location for CXXDefaultInitExpr. (#98490)
These are not "original initializers"; the single node underneath
represents the initializing node.
2024-07-13 00:38:49 -07:00
martinboehme
85f47fdd03
[clang][nullability] Improve modeling of ++/-- operators. (#96601)
We definitely know that these operations change the value of their
operand, so
clear out any value associated with it. We don't create a new value,
instead
leaving it to the analysis to do this if desired.
2024-06-26 15:03:37 +02:00
martinboehme
cfd2021410
[clang][dataflow] Add a callback run on the pre-transfer state. (#96140)
At the same time, rename `PostVisitCFG` to the more descriptive
`PostAnalysisCallbacks` (which emphasizes the fact that these callbacks
are run
after the dataflow analysis itself has converged).

Before this patch, it was only possible to run a callback on the state
_after_
the transfer function had been applied, but for many analyses, it's more
natural
to to check the state _before_ the transfer function has been applied,
because we
are usually checking the preconditions for some operation. Some checks
are
impossible to perform on the "after" state because we can no longer
check the
precondition; for example, the `++` / `--` operators on raw pointers
require the
operand to be nonnull, but after the transfer function for the operator
has been
applied, the original value of the pointer can no longer be accessed.

`UncheckedOptionalAccessModelTest` has been modified to run the
diagnosis
callback on the "before" state. In this particular case, diagnosis can
be run
unchanged on either the "before" or "after" state, but we want this test
to
demonstrate that running diagnosis on the "before" state is usually the
preferred approach.

This change is backwards-compatible; all existing analyses will continue
to run
the callback on the "after" state.
2024-06-21 06:52:31 +02:00
martinboehme
275196d866
[clang][nullability] Don't return null fields from getReferencedDecls(). (#94983)
The patch includes a repro for a case where we were returning a null
`FieldDecl`
when calling `getReferencedDecls()` on the `InitListExpr` for a union.

Also, I noticed while working on this that `RecordInitListHelper` has a
bug
where it doesn't work correctly for empty unions. This patch also
includes a
repro and fix for this bug.
2024-06-11 08:40:02 +02:00
martinboehme
282534268e
[clang][dataflow] Handle AtomicExpr in ResultObjectVisitor. (#94963)
This is one of the node kinds that should be considered an "original
initializer". The patch adds a test that was causing an assertion
failure in
`assert(Children.size() == 1)` without the fix.
2024-06-11 08:38:03 +02:00
martinboehme
492417278d
[clang][dataflow] Propagate storage location of compound assignment operators. (#94332)
To avoid generating unnecessary values, we don't create a new value but
instead
leave it to the specific analysis to do this if desired.
2024-06-04 17:08:20 +02:00
martinboehme
68761a9e05
[clang][nullability] Propagate storage location / value of ++/-- operators. (#94217)
To avoid generating unnecessary values, we don't create a new value but
instead
leave it to the specific analysis to do this if desired.
2024-06-04 08:32:29 +02:00
martinboehme
5161a3f6e5
[clang][dataflow] Rewrite getReferencedDecls() with a RecursiveASTVisitor. (#93461)
We previously had a hand-rolled recursive traversal here that was
exactly what
`RecursiveASTVistor` does anyway. Using the visitor not only eliminates
the
explicit traversal logic but also allows us to introduce a common
visitor base
class for `getReferencedDecls()` and `ResultObjectVisitor`, ensuring
that the
two are consistent in terms of the nodes they visit. Inconsistency
between these
two has caused crashes in the past when `ResultObjectVisitor` tried to
propagate
result object locations to entities that weren't modeled becasue
`getReferencedDecls()` didn't visit them.
2024-06-03 08:59:09 +02:00
Michael Kruse
f2a385c74a
[clang] Revise IDE folder structure (#89743)
Update the folder titles for targets in the monorepository that have not
seen taken care of for some time. These are the folders that targets are
organized in Visual Studio and XCode (`set_property(TARGET <target>
PROPERTY FOLDER "<title>")`) when using the respective CMake's IDE
generator.

 * Ensure that every target is in a folder
 * Use a folder hierarchy with each LLVM subproject as a top-level folder
 * Use consistent folder names between subprojects
 * When using target-creating functions from AddLLVM.cmake, automatically
deduce the folder. This reduces the number of
`set_property`/`set_target_property`, but are still necessary when
`add_custom_target`, `add_executable`, `add_library`, etc. are used. A
LLVM_SUBPROJECT_TITLE definition is used for that in each subproject's
root CMakeLists.txt.
2024-05-25 17:16:39 +02:00
martinboehme
37813e09fa
[clang][dataflow] Make CNFFormula externally accessible. (#92401)
This component can be useful when creating implementations of `Solver`,
as some
SAT solvers require the input to be in 3-CNF.

As part of making `CNFFormula` externally accessible, I have moved some
member
variables out of it that aren't really part of the representation of a
3-CNF
formula and thus live better elsewhere:

*  `WatchedHead` and `NextWatched` have been moved to
`WatchedLiteralsSolverImpl`, as they're part of the specific algorithm
used
   by that SAT solver.

* `Atomics` has become an output parameter of `buildCNF()` because it
has to do
with the relationship between a `CNFFormula` and the set of `Formula`s
it is
derived from rather than being an integral part of the representation of
a
   3-CNF formula.

I have also made all member variables private and added appropriate
accessors.
2024-05-21 11:34:08 +02:00
Samira Bazuzi
80d9ae9cbf
[clang][dataflow] Fully support Environment construction for Stmt analysis. (#91616)
Assume in fewer places that the analysis is of a `FunctionDecl`, and
initialize the `Environment` properly for `Stmt`s.

Moves constructors for `Environment` to header to make it more obvious
that there are only minor differences between them and very little
initialization in the constructors.

Tested with check-clang-tooling.
2024-05-15 16:11:11 -04:00
martinboehme
23ae482bd0
[clang][dataflow] Allow DataflowAnalysisContext to use a non-owned Solver. (#91316)
For some callers (see change in DataflowAnalysis.h), this is more
convenient.
2024-05-08 08:36:53 +02:00
martinboehme
f3fbd21fa4
[clang][dataflow] Strengthen pointer comparison. (#75170)
-  Instead of comparing the identity of the `PointerValue`s, compare the
   underlying `StorageLocation`s.

- If the `StorageLocation`s are the same, return a definite "true" as
the
result of the comparison. Before, if the `PointerValue`s were different,
we
would return an atom, even if the storage locations themselves were the
same.

- If the `StorageLocation`s are different, return an atom (as before).
Pointers
that have different storage locations may still alias, so we can't
return a
   definite "false" in this case.

The application-level gains from this are relatively modest. For the
Crubit
nullability check running on an internal codebase, this change reduces
the
number of functions on which the SAT solver times out from 223 to 221;
the
number of "pointer expression not modeled" errors reduces from 3815 to
3778.

Still, it seems that the gain in precision is generally worthwhile.

@Xazax-hun inspired me to think about this with his

[comments](https://github.com/llvm/llvm-project/pull/73860#pullrequestreview-1761484615)
on a different PR.
2024-05-07 10:12:23 +02:00
martinboehme
4d839d8f18
[clang][dataflow] Don't propagate result objects in unevaluated contexts (reland #90438) (#91172)
This relands #90348 with a fix for a [buildbot
failure](https://lab.llvm.org/buildbot/#/builders/216/builds/38446)
caused by the test being run with `-fno-rtti`.
2024-05-06 14:21:15 +02:00
martinboehme
0348e71885
[clang][dataflow] Fix crash when operator= result type is not destination type. (#90898)
The existing code was full of comments about how we assume this is
always the
case, but it's not mandated by the standard, and there is code out there
that
returns a different type. So check that the result type is in fact the
same as
the destination type before attempting to copy to the result.

To make sure that we don't bail out in more cases than intended, I've
extended
existing tests to verify that in the common case, we do return the
destination
object (by reference or value, as the case may be).
2024-05-06 08:15:12 +02:00
Weaver
2252c5c42b Revert "[clang][dataflow] Don't propagate result objects in unevaluated contexts (#90438)"
This reverts commit 597a3150e932a9423c65b5ea4b53dd431aff5865.

Caused test failure on the following buildbot:
https://lab.llvm.org/buildbot/#/builders/216/builds/38446
2024-05-02 11:51:45 +01:00
martinboehme
597a3150e9
[clang][dataflow] Don't propagate result objects in unevaluated contexts (#90438)
Trying to do so can cause crashes -- see newly added test and the
comments in
the fix.

We're starting to see a repeating pattern here: We're getting crashes
because
`ResultObjectVisitor` and `getReferencedDecls()` don't agree on which
parts of
the AST to visit and, hence, which fields should be modeled.

I think we should ensure consistency between these two parts of the code
by
using a `RecursiveASTVisitor` in `getReferencedDecls()`[^1]; the
`Traverse...()` functions that control which parts of the AST we visit
would go
in a common base class that would be used for both `ResultObjectVisitor`
and
`getReferencedDecls()`.

I'd like to focus this PR, however, on a targeted fix for the current
crash and
postpone the refactoring to a later PR (which will be easier to revert
if there
are unintended side-effects).

[^1]: As an added bonus, this would make the code better structured and
more
efficient than the current sequence of `if (dyn_cast<T>(...))`
statements).
2024-05-02 08:35:13 +02:00
martinboehme
c70f058316
[clang][dataflow] Fix crash when ConstantExpr is used in conditional operator. (#90112)
`ConstantExpr` does not appear as a `CFGStmt` in the CFG, so
`StmtToEnvMap::getEnvironment()` was not finding an entry for it in the
map,
causing a crash when we tried to access the iterator resulting from the
map
lookup.

The fix is to make `ignoreCFGOmittedNodes()` ignore `ConstantExpr`, but
in
addition, I'm hardening `StmtToEnvMap::getEnvironment()` to make sure
release
builds don't crash in similar situations in the future.
2024-04-26 09:30:07 +02:00
martinboehme
b9208ce318
[clang][dataflow] Crash fix for widenDistinctValues(). (#89895)
We used to crash if the previous iteration contained a `BoolValue` and
the
current iteration contained an `IntegerValue`. The accompanying test
sets up
this situation -- see comments there for details.

While I'm here, clean up the tests for integral casts to use the test
helpers we
have available now. I was looking at these tests to understand how we
handle
integral casts, and the test helpers make the tests easier to read.
2024-04-25 09:24:08 +02:00
martinboehme
9b0651f5ae
[clang][dataflow] Don't propagate result objects in nested declarations. (#89903)
Trying to do so can cause crashes -- see newly added test and the
comments in
the fix.
2024-04-25 09:22:14 +02:00
martinboehme
9ba6961ce0
Reapply "[clang][dataflow] Model conditional operator correctly." with fixes (#89596)
I reverted https://github.com/llvm/llvm-project/pull/89213 beause it was
causing buildbots to fail with assertion failures.

Embarrassingly, it turns out I had been running tests locally in
`Release` mode, i.e. with `assert()` compiled away.

This PR re-lands #89213 with fixes for the failing assertions.
2024-04-23 08:10:55 +02:00
martinboehme
8ff6434546
Revert "[clang][dataflow] Model conditional operator correctly." (#89577)
Reverts llvm/llvm-project#89213

This is causing buildbot failures.
2024-04-22 09:35:29 +02:00
martinboehme
abb958f161
[clang][dataflow] Model conditional operator correctly. (#89213) 2024-04-22 09:23:13 +02:00
Samira Bazuzi
d634b23364
[clang][dataflow] Expose getReferencedDecls for a Stmt. (#89444) 2024-04-19 16:23:43 -04:00
martinboehme
1412210632
[clang][dataflow][NFC] Fix code formatting in DataflowEnvironment.cpp (#89352)
For some reason, when I merged #89235, two lines were mis-formatted.

This patch corrects this; while I'm here, I'm also correcting other
existing formatting errors.
2024-04-19 10:12:57 +02:00
martinboehme
e8fce95887
[clang][nullability] Remove RecordValue. (#89052)
This class no longer serves any purpose; see also the discussion here:
https://reviews.llvm.org/D155204#inline-1503204

A lot of existing tests in TransferTest.cpp check for the existence of
`RecordValue`s. Some of these checks are now simply redundant and have
been
removed. In other cases, tests were checking for the existence of a
`RecordValue` as a way of testing whether a record has been initialized.
I have
typically changed these test to instead check whether a field of the
record has
a value.
2024-04-19 09:39:52 +02:00
martinboehme
ca7d9442ba
[clang][dataflow] Support CXXParenListInitExpr in PropagateResultObject(). (#89235) 2024-04-19 09:06:13 +02:00
martinboehme
b5f2cecf83
Revert "[clang][dataflow] Refactor PropagateResultObject() with a switch statement." (#89176)
Reverts llvm/llvm-project#88865

There were failing tests in the CI that I didn't notice. Sorry.
2024-04-18 09:23:03 +02:00
martinboehme
da579ad807
[clang][dataflow] Refactor PropagateResultObject() with a switch statement. (#88865)
See also discussion in #88726.
2024-04-18 09:21:10 +02:00
Timm Baeder
3d56ea05b6
[clang][NFC] Fix FieldDecl::isUnnamedBitfield() capitalization (#89048)
We always capitalize bitfield as "BitField".
2024-04-18 07:39:29 +02:00
martinboehme
1bccbe1f49
[clang][dataflow] Treat BuiltinBitCastExpr correctly in PropagateResultObject(). (#88875)
This patch includes a test that assert-fails without the fix.
2024-04-17 08:17:56 +02:00
martinboehme
b851c7f1fc
[clang][dataflow] Support StmtExpr in PropagateResultObject(). (#88872)
This patch adds a test that assert-fails without the fix.
2024-04-17 08:05:43 +02:00
Samira Bazuzi
9ec8c96166
[clang][dataflow] Expose getReferencedDecls and relocate free functions. (#88754)
Moves free functions from DataflowEnvironment.h/cc and
DataflowAnalysisContext.h/cc to RecordOps and a new ASTOps and exposes
them as needed for current use and to expose getReferencedDecls for
out-of-tree use.

Minimal change in functionality, only to modify the return type of
getReferenceDecls to return the collected decls instead of using output
params.

Tested with `ninja check-clang-tooling`.
2024-04-16 14:46:05 -04:00
martinboehme
3c6f91e5b6
[clang][dataflow] Fix result object location for builtin <=>. (#88726)
The newly added test causes an assertion failure in
`PropagateResultObject()`
without the fix added here.
2024-04-16 08:49:45 +02:00
martinboehme
71f1932b84
[clang][dataflow] Reland #87320: Propagate locations from result objects to initializers. (#88316)
This relands #87320 and additionally removes the now-unused function
`isOriginalRecordConstructor()`, which was causing buildbots to fail.
2024-04-11 08:20:35 +02:00
martinboehme
7549b45825
Revert "[clang][dataflow] Propagate locations from result objects to initializers." (#88315)
Reverts llvm/llvm-project#87320

This is causing buildbots to fail because
`isOriginalRecordConstructor()` is now unused.
2024-04-10 21:27:10 +02:00
martinboehme
21009f466e
[clang][dataflow] Propagate locations from result objects to initializers. (#87320)
Previously, we were propagating storage locations the other way around,
i.e.
from initializers to result objects, using `RecordValue::getLoc()`. This
gave
the wrong behavior in some cases -- see the newly added or fixed tests
in this
patch.

In addition, this patch now unblocks removing the `RecordValue` class
entirely,
as we no longer need `RecordValue::getLoc()`.

With this patch, the test `TransferTest.DifferentReferenceLocInJoin`
started to
fail because the framework now always uses the same storge location for
a
`MaterializeTemporaryExpr`, meaning that the code under test no longer
set up
the desired state where a variable of reference type is mapped to two
different
storage locations in environments being joined. Rather than trying to
modify
this test to set up the test condition again, I have chosen to replace
the test
with an equivalent test in DataflowEnvironmentTest.cpp that sets up the
test
condition directly; because this test is more direct, it will also be
less
brittle in the face of future changes.
2024-04-10 20:03:35 +02:00
NAKAMURA Takumi
d08a76d1ac Fix warnings discovered by #87348 [-Wunused-but-set-variable] 2024-04-07 11:02:08 +09:00
Yitzhak Mandelbaum
bbd259af0a
[clang][dataflow] Refactor widen API to be explicit about change effect. (#87233)
The previous API relied on pointer equality of inputs and outputs to
signal whether a change occured. This was too subtle and led to bugs in
practice. It was also very limiting: the override could not return an equivalent (but
not identical) value.
2024-04-04 08:39:51 -04:00