aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Analysis/CFG.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-07-01[Analysis] Use range-based for loops (NFC) (#146466)Kazu Hirata1-3/+2
2025-07-01[Analysis] Remove an unnecessary cast (NFC) (#146547)Kazu Hirata1-2/+1
ME is already of ObjCMessageExpr *.
2025-06-10[clang][CFG] Fix assertion failure in checkIncorrectLogicOperator (#142897)Ziqing Luo1-0/+22
`checkIncorrectLogicOperator` checks if an expression, for example `x != 0 || x != 1.0`, is always true or false by comparing the two literals `0` and `1.0`. But in case `x` is a 16-bit float, the two literals have distinct types---16-bit float and double, respectively. Directly comparing `APValue`s extracted from the two literals results in an assertion failure because of their distinct types. This commit fixes the issue by doing a conversion from the "smaller" one to the "bigger" one. The two literals must be compatible because both of them are comparing with `x`. rdar://152456316
2025-05-31[Analysis] Remove unused includes (NFC) (#142255)Kazu Hirata1-1/+0
These are identified by misc-include-cleaner. I've filtered out those that break builds. Also, I'm staying away from llvm-config.h, config.h, and Compiler.h, which likely cause platform- or compiler-specific build failures.
2025-05-12Reland [Clang][analyzer] replace Stmt* with ConstCFGElement in ↵Fangyi Zhou1-20/+26
SymbolConjured (#137355) Closes #57270. This PR changes the `Stmt *` field in `SymbolConjured` with `CFGBlock::ConstCFGElementRef`. The motivation is that, when conjuring a symbol, there might not always be a statement available, causing information to be lost for conjured symbols, whereas the CFGElementRef can always be provided at the callsite. Following the idea, this PR changes callsites of functions to create conjured symbols, and replaces them with appropriate `CFGElementRef`s. There is a caveat at loop widening, where the correct location is the CFG terminator (which is not an element and does not have a ref). In this case, the first element in the block is passed as a location. Previous PR #128251, Reverted at #137304.
2025-05-10[Clang] Improve `-Wtautological-overlap-compare` diagnostics flag (#133653)Yutong Zhu1-74/+112
This PR attempts to improve the diagnostics flag `-Wtautological-overlap-compare` (#13473). I have added code to warn about float-point literals and character literals. I have also changed the warning message for the non-overlapping case to provide a more correct hint to the user. Fixes #13473.
2025-04-25Revert "[Clang][analyzer] replace Stmt* with ConstCFGElementRef in ↵Balazs Benics1-26/+20
SymbolConjured" (#137304) Reverts llvm/llvm-project#128251 ASAN bots reported some errors: https://lab.llvm.org/buildbot/#/builders/55/builds/10398 Reverting for investigation. ``` Failed Tests (6): Clang :: Analysis/loop-widening-ignore-static-methods.cpp Clang :: Analysis/loop-widening-notes.cpp Clang :: Analysis/loop-widening-preserve-reference-type.cpp Clang :: Analysis/loop-widening.c Clang :: Analysis/loop-widening.cpp Clang :: Analysis/this-pointer.cpp Testing Time: 411.55s Total Discovered Tests: 118563 Skipped : 33 (0.03%) Unsupported : 2015 (1.70%) Passed : 116291 (98.08%) Expectedly Failed: 218 (0.18%) Failed : 6 (0.01%) FAILED: CMakeFiles/check-all /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/CMakeFiles/check-all cd /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan && /usr/bin/python3 /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/./bin/llvm-lit -sv --param USE_Z3_SOLVER=0 /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/utils/mlgo-utils /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/lld/test /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/mlir/test /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/clang/test /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/utils/lit /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/test ninja: build stopped: subcommand failed. ``` ``` /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/lib/clang/21/include -nostdsysteminc -analyze -analyzer-constraints=range -setup-static-analyzer -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/test/Analysis/loop-widening.c # RUN: at line 1 + /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/lib/clang/21/include -nostdsysteminc -analyze -analyzer-constraints=range -setup-static-analyzer -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/test/Analysis/loop-widening.c PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script. Stack dump: 0. Program arguments: /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/lib/clang/21/include -nostdsysteminc -analyze -analyzer-constraints=range -setup-static-analyzer -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/test/Analysis/loop-widening.c 1. <eof> parser at end of file 2. While analyzing stack: #0 Calling nested_loop_inner_widen #0 0x0000c894cca289cc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/lib/Support/Unix/Signals.inc:804:13 #1 0x0000c894cca23324 llvm::sys::RunSignalHandlers() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/lib/Support/Signals.cpp:106:18 #2 0x0000c894cca29bbc SignalHandler(int, siginfo_t*, void*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/lib/Support/Unix/Signals.inc:0:3 #3 0x0000f6898da4a8f8 (linux-vdso.so.1+0x8f8) #4 0x0000f6898d377608 (/lib/aarch64-linux-gnu/libc.so.6+0x87608) #5 0x0000f6898d32cb3c raise (/lib/aarch64-linux-gnu/libc.so.6+0x3cb3c) #6 0x0000f6898d317e00 abort (/lib/aarch64-linux-gnu/libc.so.6+0x27e00) #7 0x0000c894c5e77fec __sanitizer::Atexit(void (*)()) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp:168:10 #8 0x0000c894c5e76680 __sanitizer::Die() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_termination.cpp:52:5 #9 0x0000c894c5e69650 Unlock /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_mutex.h:250:16 #10 0x0000c894c5e69650 ~GenericScopedLock /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_mutex.h:386:51 #11 0x0000c894c5e69650 __hwasan::ScopedReport::~ScopedReport() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan_report.cpp:54:5 #12 0x0000c894c5e68de0 __hwasan::(anonymous namespace)::BaseReport::~BaseReport() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan_report.cpp:476:7 #13 0x0000c894c5e66b74 __hwasan::ReportTagMismatch(__sanitizer::StackTrace*, unsigned long, unsigned long, bool, bool, unsigned long*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan_report.cpp:1091:1 #14 0x0000c894c5e52cf8 Destroy /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_common.h:532:31 #15 0x0000c894c5e52cf8 ~InternalMmapVector /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_common.h:642:56 #16 0x0000c894c5e52cf8 __hwasan::HandleTagMismatch(__hwasan::AccessInfo, unsigned long, unsigned long, void*, unsigned long*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan.cpp:245:1 #17 0x0000c894c5e551c8 __hwasan_tag_mismatch4 /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan.cpp:764:1 #18 0x0000c894c5e6a2f8 __interception::InterceptFunction(char const*, unsigned long*, unsigned long, unsigned long) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/interception/interception_linux.cpp:60:0 #19 0x0000c894d166f664 getBlock /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h:217:45 #20 0x0000c894d166f664 getCFGElementRef /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h:230:59 #21 0x0000c894d166f664 clang::ento::ExprEngine::processCFGBlockEntrance(clang::BlockEdge const&, clang::ento::NodeBuilderWithSinks&, clang::ento::ExplodedNode*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp:2570:45 #22 0x0000c894d15f3a1c hasGeneratedNodes /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h:333:37 #23 0x0000c894d15f3a1c clang::ento::CoreEngine::HandleBlockEdge(clang::BlockEdge const&, clang::ento::ExplodedNode*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:319:20 #24 0x0000c894d15f2c34 clang::ento::CoreEngine::dispatchWorkItem(clang::ento::ExplodedNode*, clang::ProgramPoint, clang::ento::WorkListUnit const&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:220:7 #25 0x0000c894d15f2398 operator-> /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/libcxx_install_hwasan/include/c++/v1/__memory/unique_ptr.h:267:101 #26 0x0000c894d15f2398 clang::ento::CoreEngine::ExecuteWorkList(clang::LocationContext const*, unsigned int, llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>)::$_0::operator()(unsigned int) const /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:140:12 #27 0x0000c894d15f14b4 clang::ento::CoreEngine::ExecuteWorkList(clang::LocationContext const*, unsigned int, llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:165:7 #28 0x0000c894d0ebb9dc release /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:232:9 #29 0x0000c894d0ebb9dc ~IntrusiveRefCntPtr /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:196:27 #30 0x0000c894d0ebb9dc ExecuteWorkList /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h:192:5 #31 0x0000c894d0ebb9dc RunPathSensitiveChecks /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:772:7 #32 0x0000c894d0ebb9dc (anonymous namespace)::AnalysisConsumer::HandleCode(clang::Decl*, unsigned int, clang::ento::ExprEngine::InliningModes, llvm::DenseSet<clang::Decl const*, llvm::DenseMapInfo<clang::Decl const*, void>>*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:741:5 #33 0x0000c894d0eb6ee4 begin /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/ADT/DenseMap.h:0:0 #34 0x0000c894d0eb6ee4 begin /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/ADT/DenseSet.h:187:45 #35 0x0000c894d0eb6ee4 HandleDeclsCallGraph /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:516:29 #36 0x0000c894d0eb6ee4 runAnalysisOnTranslationUnit /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:584:5 #37 0x0000c894d0eb6ee4 (anonymous namespace)::AnalysisConsumer::HandleTranslationUnit(clang::ASTContext&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:647:3 #38 0x0000c894d18a7a38 clang::ParseAST(clang::Sema&, bool, bool) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/Parse/ParseAST.cpp:0:13 #39 0x0000c894ce81ed70 clang::FrontendAction::Execute() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1231:10 #40 0x0000c894ce6f2144 getPtr /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/Support/Error.h:278:42 #41 0x0000c894ce6f2144 operator bool /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/Support/Error.h:241:16 #42 0x0000c894ce6f2144 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1058:23 #43 0x0000c894cea718cc operator-> /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/libcxx_install_hwasan/include/c++/v1/__memory/shared_ptr.h:635:12 #44 0x0000c894cea718cc getFrontendOpts /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/Frontend/CompilerInstance.h:307:12 #45 0x0000c894cea718cc clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:301:14 #46 0x0000c894c5e9cf28 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/tools/driver/cc1_main.cpp:294:15 #47 0x0000c894c5e92a9c ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/tools/driver/driver.cpp:223:12 #48 0x0000c894c5e902ac clang_main(int, char**, llvm::ToolContext const&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/tools/driver/driver.cpp:0:12 #49 0x0000c894c5eb2e34 main /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/clang/tools/driver/clang-driver.cpp:17:3 #50 0x0000f6898d3184c4 (/lib/aarch64-linux-gnu/libc.so.6+0x284c4) #51 0x0000f6898d318598 __libc_start_main (/lib/aarch64-linux-gnu/libc.so.6+0x28598) #52 0x0000c894c5e52a30 _start (/home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang+0x6512a30) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/clang/test/Analysis/Output/loop-widening.c.script: line 2: 2870204 Aborted /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/lib/clang/21/include -nostdsysteminc -analyze -analyzer-constraints=range -setup-static-analyzer -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/test/Analysis/loop-widening.c ```
2025-04-25[Clang][analyzer] Replace Stmt* with ConstCFGElementRef in SymbolConjured ↵Fangyi Zhou1-20/+26
(#128251) This PR changes the `Stmt *` field in `SymbolConjured` with `CFGBlock::ConstCFGElementRef`. The motivation is that, when conjuring a symbol, there might not always be a statement available, causing information to be lost for conjured symbols, whereas the CFGElementRef can always be provided at the callsite. Following the idea, this PR changes callsites of functions to create conjured symbols, and replaces them with appropriate `CFGElementRef`s. Closes #57270
2025-04-19[clang] Use llvm::SmallVector::pop_back_val (NFC) (#136451)Kazu Hirata1-2/+1
2025-03-06Reapply "[analyzer] Handle [[assume(cond)]] as __builtin_assume(cond)" (#129234)Balazs Benics1-14/+57
This is the second attempt to bring initial support for [[assume()]] in the Clang Static Analyzer. The first attempt (#116462) was reverted in 2b9abf0db2d106c7208b4372e662ef5df869e6f1 due to some weird failure in a libcxx test involving `#pragma clang loop vectorize(enable) interleave(enable)`. The failure could be reduced into: ```c++ template <class ExecutionPolicy> void transform(ExecutionPolicy) { #pragma clang loop vectorize(enable) interleave(enable) for (int i = 0; 0;) { // The DeclStmt of "i" would be added twice in the ThreadSafety analysis. // empty } } void entrypoint() { transform(1); } ``` As it turns out, the problem with the initial patch was this: ```c++ for (const auto *Attr : AS->getAttrs()) { if (const auto *AssumeAttr = dyn_cast<CXXAssumeAttr>(Attr)) { Expr *AssumeExpr = AssumeAttr->getAssumption(); if (!AssumeExpr->HasSideEffects(Ctx)) { childrenBuf.push_back(AssumeExpr); } } // Visit the actual children AST nodes. // For CXXAssumeAttrs, this is always a NullStmt. llvm::append_range(childrenBuf, AS->children()); // <--- This was not meant to be part of the "for" loop. children = childrenBuf; } return; ``` The solution was simple. Just hoist it from the loop. I also had a closer look at `CFGBuilder::VisitAttributedStmt`, where I also spotted another bug. We would have added the CFG blocks twice if the AttributedStmt would have both the `[[fallthrough]]` and the `[[assume()]]` attributes. With my fix, it will only once add the blocks. Added a regression test for this. Co-authored-by: Vinay Deshmukh <vinay_deshmukh AT outlook DOT com>
2025-02-22Revert "Reapply "[Analyzer][CFG] Correctly handle rebuilt default arg and ↵yronglin1-45/+9
default init expression" (#127338)" (#128205) This reverts commit d235b72178adc710bf704078fbe0cd687642f3e0.
2025-02-17Reapply "[Analyzer][CFG] Correctly handle rebuilt default arg and default ↵yronglin1-9/+45
init expression" (#127338) This PR reapply https://github.com/llvm/llvm-project/pull/117437. The issue has been fixed by the 2nd commit, we need to ignore parens in CXXDefaultArgExpr when build CFG, because CXXDefaultArgExpr::getExpr stripped off the top level FullExpr and ConstantExpr, ParenExpr may occurres in the top level. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-02-07[analyzer] Do not destruct fields of unions (#122330)Jameson Nash1-0/+2
The C++ standard prohibits this implicit destructor call, leading to incorrect reports from clang-analyzer. This causes projects that use std::option (including llvm) to fail the cplusplus.NewDelete test incorrectly when run through the analyzer. Fixes #119415
2025-02-03Revert "[Analyzer][CFG] Correctly handle rebuilt default arg and default ↵Hans Wennborg1-41/+9
init expression (#117437)" This caused assertion failures: clang/lib/Analysis/CFG.cpp:822: void (anonymous namespace)::CFGBuilder::appendStmt(CFGBlock *, const Stmt *): Assertion `!isa<Expr>(S) || cast<Expr>(S)->IgnoreParens() == S' failed. See comment on the PR. This reverts commit 44aa618ef67d302f5ab77cc591fb3434fe967a2e.
2025-02-01[Analyzer][CFG] Correctly handle rebuilt default arg and default init ↵yronglin1-9/+41
expression (#117437) Clang currently support extending lifetime of object bound to reference members of aggregates, that are created from default member initializer. This PR address this change and updaye CFG and ExprEngine. This PR reapply https://github.com/llvm/llvm-project/pull/91879. Fixes https://github.com/llvm/llvm-project/issues/93725. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2024-12-19Revert "[analyzer] Handle [[assume(cond)]] as __builtin_assume(cond) (#116462)"Thurston Dang1-52/+20
This reverts commit 89da344e5879e5347b5057520d5230e40ae24831. Reason: buildbot breakages e.g., https://lab.llvm.org/buildbot/#/builders/55/builds/4556 (for which the reverted patch is the only code change)
2024-12-19[analyzer] Handle [[assume(cond)]] as __builtin_assume(cond) (#116462)Vinay Deshmukh1-20/+52
Resolves #100762 Gist of the change: 1. All the symbol analysis, constraint manager and expression parsing logic was already present, but the previous code didn't "visit" the expressions within `assume()` by parsing those expressions, all of the code "just works" by evaluating the SVals, and hence leaning on the same logic that makes the code with `__builtin_assume` work 2. "Ignore" an expression from adding in CFG if it has side-effects ( similar to CGStmt.cpp (todo add link)) 3. Add additional test case for ternary operator handling and modify CFG.cpp's VisitGuardedExpr code for `continue`-ing if the `ProgramPoint` is a `StmtPoint` --------- Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
2024-11-21[Clang] Handle `[[noreturn]]` constructors in CFG (#115558)Oleksandr T.1-11/+15
Fixes #63009.
2024-11-20[Clang] avoid adding consteval condition as the last statement to preserve ↵Oleksandr T.1-1/+4
valid CFG (#116513) Fixes #116485
2024-09-14[clang] Nits on uses of raw_string_ostream (NFC)JOE19941-4/+3
* Don't call raw_string_ostream::flush(), which is essentially a no-op. * Strip unneeded calls to raw_string_ostream::str(), to avoid extra indirection.
2024-06-06Revert "[Analyzer][CFG] Correctly handle rebuilt default arg and default ↵bgra81-41/+9
init expression (#91879)" (#94597) This depends on https://github.com/llvm/llvm-project/pull/92527 which needs to be reverted due to https://github.com/llvm/llvm-project/pull/92527#issuecomment-2149120420. This reverts commit 905b402a5d8f1490d668f40942390ebd6e87aa8f. Co-authored-by: Bogdan Graur <bgraur@google.com>
2024-05-23[Analyzer][CFG] Correctly handle rebuilt default arg and default init ↵yronglin1-9/+41
expression (#91879) Depends on https://github.com/llvm/llvm-project/pull/92527 Clang now support the following: - Extending lifetime of object bound to reference members of aggregates, that are created from default member initializer. - Rebuild `CXXDefaultArgExpr` and `CXXDefaultInitExpr` as needed where called or constructed. But CFG and ExprEngine need to be updated to address this change. This PR add `CXXDefaultArgExpr` and `CXXDefaultInitExpr` into CFG, and correct handle these expressions in ExprEngine --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2024-03-26[NFC] Refactor ConstantArrayType size storage (#85716)Chris B1-2/+2
In PR #79382, I need to add a new type that derives from ConstantArrayType. This means that ConstantArrayType can no longer use `llvm::TrailingObjects` to store the trailing optional Expr*. This change refactors ConstantArrayType to store a 60-bit integer and 4-bits for the integer size in bytes. This replaces the APInt field previously in the type but preserves enough information to recreate it where needed. To reduce the number of places where the APInt is re-constructed I've also added some helper methods to the ConstantArrayType to allow some common use cases that operate on either the stored small integer or the APInt as appropriate. Resolves #85124.
2024-01-30[clang][NFC] Use no-param version of skipRValueSubobjectAdjustmentsTimm Bäder1-4/+1
when possible.
2023-09-19[clang][CFG] Cleanup functionsTimm Bäder1-11/+29
Add declarations declared with attribute(cleanup(...)) to the CFG, similar to destructors. Differential Revision: https://reviews.llvm.org/D157385
2023-08-17[clang][Analysis] Handle && and || against variable and its negation as ↵Takuya Shimizu1-5/+30
tautology This patch introduces a new warning flag -Wtautological-negation-compare grouped in -Wtautological-compare that warns on the use of && or || operators against a variable and its negation. e.g. x || !x and !x && x This also makes the -Winfinite-recursion diagnose more cases. Fixes https://github.com/llvm/llvm-project/issues/56035 Differential Revision: https://reviews.llvm.org/D152093
2023-07-27[analyzer] Remove rdar links from static analyzer and libAnalysis sources. NFC.Artem Dergachev1-2/+1
I actually visited each link and added relevant context directly to the code. This is related to the effort to eliminate internal bug tracker links (d618f1c, e0ac46e). Test files still have a lot of rdar links and ids in them. I haven't touched them yet.
2023-07-19[analyzer] Model lifetime of a variable declared in for condition in CFG ↵tomasz-kaminski-sonarsource1-2/+5
correctly Per [stmt.for] p1 (https://eel.is/c++draft/stmt.for#1) the following `for` and `while` statements are equivalent ``` for (; A c = b; b.c) { A d; } while (A c = b) { A d; b.c; } ``` As a consequence, the variable declared for the condition expression should be destroyed after the increment expression. This fixed the handling of the object lifetime `continue`, and now destructors, scope and lifetime elements are present for continue path in following code: ``` for (; A c = b; b.c) { if (cond) continue; A d; } ``` Reviewed By: xazax.hun Differential Revision: https://reviews.llvm.org/D155547
2023-07-18[analyzer] Rework support for CFGScopeBegin, CFGScopeEnd, CFGLifetime elementstomasz-kaminski-sonarsource1-224/+196
This patch reworks generation for the `CFGScopeBegin`, `CFGScopeEnd`, and `CFGLiftimeEnd`, in a way that they are now compatible with each other and `CFGAutomaticObjDtor`. All of the above elements are now generated by a single code path, that conditionally inserts elements if they are requested. In addition, the handling of `goto` statements is improved. The `goto` statement may leave multiple scopes (and trigger destruction and lifetime end for the affected variables) and enter multiple scopes, for example: ```lang=C++ { int s1; { int s2; goto label; // leaves s1, s2, and enters t1 t1 } } { int t1; { int t2; label: } } ``` This is performed by first determining the shared parent scope of the source and destination. And then emitting elements for exiting each scope between the source and the parent, and entering each scope between the parent and destination. All such elements are appended to the source block, as one label may be reached from multiple scopes. Finally, the approach for handling backward jumps is changed. When connecting a source block to a destination block that requires the insertion of additional elements, we put this element into a new block, which is then linked between the source and the destination block. For example: ```lang=C++ { int t; label: // Destination block referred to as 'DB' } { // Source block referred to as 'SB' Obj s; goto label; } ``` The jump between `SB` with terminator `T: goto` and `DB` should be coupled with the following CFG elements: ``` CFGAutomaticObjDtor(s) CFGLifetimeEnd(s) CFGScopeEnd(s) CFGScopeBegin(t) ``` To handle such situations, we create a new link (`LB`) that is linked as the predecessor of `DB`, to which we transfer the terminator (`goto` statement) of `SB`. Then `LB` is handled in the same manner as the source block in the case of forward jumps. This produces CFG that looks like this: ``` SB -> LB (T: goto) -> DB ``` Finally, the resulting block is linked as the successor of `SB`. Such an approach uses existing handling of the `noreturn` destructors. As a reminder, for each destructor of an automatic object that is marked as `noreturn`, a new `noreturn` block (marked `NBn`) is created, at the destructor is inserted at the end of it. To illustrate, given two `noreturn` destructors, we will have: ``` SB -> NB1 (noreturn) NB2 (noreturn) LB (T:goto) -> DB ``` Reviewed By: ymandel, steakhal Differential Revision: https://reviews.llvm.org/D153273
2023-07-17Revert "Remove rdar links; NFC"Mehdi Amini1-1/+2
This reverts commit d618f1c3b12effd0c2bdb7d02108d3551f389d3d. This commit wasn't reviewed ahead of time and significant concerns were raised immediately after it landed. According to our developer policy this warrants immediate revert of the commit. https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy Differential Revision: https://reviews.llvm.org/D155509
2023-07-10[NFC] Initialize pointer fields and remove needless null check.Sindhu Chittireddy1-1/+1
Reviewed here: https://reviews.llvm.org/D153589
2023-07-07Remove rdar links; NFCAaron Ballman1-2/+1
This removes links to rdar, which is an internal bug tracker that the community doesn't have visibility into. See further discussion at: https://discourse.llvm.org/t/code-review-reminder-about-links-in-code-commit-messages/71847
2023-06-27Revert "[clang][CFG][NFC] A few smaller cleanups"Timm Bäder1-5/+5
This reverts commit 173df3dd5f9a812b07f9866965f4e92a982a3fca. Looks like this wasn't as innocent as it seemed: https://lab.llvm.org/buildbot#builders/38/builds/12982
2023-06-27[clang][CFG][NFC] A few smaller cleanupsTimm Bäder1-5/+5
Use dyn_cast_if_present instead of _or_null, use decomposition decls, and a few other minor things.
2023-05-31[clang][analyzer][NFC] Use the operator new directly with the `BumpPtrAllocator`Dmitri Gribenko1-4/+2
Reviewed By: xazax.hun Differential Revision: https://reviews.llvm.org/D151818
2023-03-25[Analysis] Fix use-after-scope in CFGElement dumpSam McCall1-1/+2
2023-03-15[clang] Use *{Map,Set}::contains (NFC)Kazu Hirata1-2/+1
2023-01-14[clang] Remove remaining uses of llvm::Optional (NFC)Kazu Hirata1-1/+0
This patch removes several "using" declarations and #include "llvm/ADT/Optional.h". This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14[clang] Use std::optional instead of llvm::Optional (NFC)Kazu Hirata1-6/+7
This patch replaces (llvm::|)Optional< with std::optional<. I'll post a separate patch to remove #include "llvm/ADT/Optional.h". This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14[clang] Add #include <optional> (NFC)Kazu Hirata1-0/+1
This patch adds #include <optional> to those files containing llvm::Optional<...> or Optional<...>. I'll post a separate patch to actually replace llvm::Optional with std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-09Move from llvm::makeArrayRef to ArrayRef deduction guides - clang/ partserge-sans-paille1-2/+2
This is a follow-up to https://reviews.llvm.org/D140896, split into several parts as it touches a lot of files. Differential Revision: https://reviews.llvm.org/D141139
2022-12-03[Analysis] Use std::nullopt instead of None (NFC)Kazu Hirata1-2/+2
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02Use CTAD on llvm::SaveAndRestoreJan Svoboda1-37/+35
Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D139229
2022-11-07[clang][NFC] Use c++17 style variable type traitsNathan James1-3/+3
This was done as a test for D137302 and it makes sense to push these changes Reviewed By: shafik Differential Revision: https://reviews.llvm.org/D137491
2022-09-28Fix a tautological comparison bug caught during post-commitAaron Ballman1-2/+2
This amends fd874e5fb119e1d9f427a299ffa5bbabaeba9455 to correctly set the bit width of a '!' operator to be the same width as an 'int'. This fixes a failed assertion about unexpected bit widths that was reported during post-commit testing.
2022-09-03[clang] Qualify auto in range-based for loops (NFC)Kazu Hirata1-1/+1
2022-08-24[analyzer] Process non-POD array element destructorsisuckatcs1-3/+15
The constructors of non-POD array elements are evaluated under certain conditions. This patch makes sure that in such cases we also evaluate the destructors. Differential Revision: https://reviews.llvm.org/D130737
2022-08-22[analyzer] Handling non-POD multidimensional arrays in ArrayInitLoopExprisuckatcs1-4/+18
This patch makes it possible for lambdas, implicit copy/move ctors and structured bindings to handle non-POD multidimensional arrays. Differential Revision: https://reviews.llvm.org/D131840
2022-08-19Missing tautological compare warnings due to unary operatorsMuhammad Usman Shahid1-12/+57
The patch mainly focuses on the no warnings for -Wtautological-compare. It work fine for the positive numbers but doesn't for the negative numbers. This is because the warning explicitly checks for an IntegerLiteral AST node, but -1 is represented by a UnaryOperator with an IntegerLiteral sub-Expr. Fixes #42918 Differential Revision: https://reviews.llvm.org/D130510
2022-08-16[CFG] Fix crash on CFG building when deriving from a template.Clement Courbet1-2/+2
Differential Revision: https://reviews.llvm.org/D121365