aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/AnalysisBasedWarnings.cpp
AgeCommit message (Collapse)AuthorFilesLines
2022-12-05Revert "[-Wunsafe-buffer-usage] Initial commit - Transition away from raw ↵Artem Dergachev1-24/+0
buffers." This reverts commit 200007ec85f81122fd260a4e68308e54607ca37a.
2022-12-05[-Wunsafe-buffer-usage] Initial commit - Transition away from raw buffers.Artem Dergachev1-0/+24
This is the initial commit for -Wunsafe-buffer-usage, a warning that helps codebases (especially modern C++ codebases) transition away from raw buffer pointers. The warning is implemented in libAnalysis as it's going to become a non-trivial analysis, mostly the fixit part where we try to figure out if we understand a variable's use pattern well enough to suggest a safe container/view as a replacement. Some parts of this analsysis may eventually prove useful for any similar fixit machine that tries to change types of variables. The warning is disabled by default. RFC/discussion in https://discourse.llvm.org/t/rfc-c-buffer-hardening/65734 Differential Revision: https://reviews.llvm.org/D137346
2022-07-23Use the range-based overload of llvm::sort where possibleDmitri Gribenko1-2/+1
Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D130403
2022-05-10[NFC] Replace not-null and not-isa check with a not-isa_and_nonnullErich Keane1-1/+1
2022-04-29Thread safety analysis: Don't pass capability kind where not needed (NFC)Aaron Puchert1-4/+3
If no capability is held, or the capability expression is invalid, there is obviously no capability kind and so none would be reported. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D124132
2022-01-02[clang] Remove redundant member initialization (NFC)Kazu Hirata1-1/+1
Identified with readability-redundant-member-init.
2021-11-28[clang] Fix -Wreturn-type false positive in @try statementsNico Weber1-1/+1
After 04f30795f16638, -Wreturn-type has an effect on functions that contain @try/@catch statements. CheckFallThrough() was missing a case for ObjCAtTryStmts, leading to a false positive. (What about the other two places in CheckFallThrough() that handle CXXTryStmt but not ObjCAtTryStmts? - I think the last use of CXXTryStmt is dead in practice: 04c6851cd made it so that calls never add edges to try bodies, and the CFG block for a try statement is always an empty block containing just the try element itself as terminator (the try body itself is part of the normal flow of the function and not connected to the block for the try statement itself. The try statment cfg block is only connected to the catch bodies, and only reachable from throw expressions within the try body.) - The first use of CXXTryStmt might be important. It looks similar to the code that adds all cfg blocks for try statements as roots of the reachability graph for the reachability warnings, but I can't find a way to trigger it. So I'm omitting it for now. The CXXTryStmt code path seems to only be hit by try statements that are function bodies without a surrounding compound statements (`f() try { ... } catch ...`), and those don't exist for ObjC @try statements. ) Fixes PR52473. Differential Revfision: https://reviews.llvm.org/D114660
2021-11-17[Format, Sema] Use range-based for loops with llvm::reverse (NFC)Kazu Hirata1-9/+4
2021-10-24Use llvm::any_of and llvm::none_of (NFC)Kazu Hirata1-1/+1
2021-10-12[clang/CFG] Don't explicitly add AttributedStmtClass to AlwaysAddListNico Weber1-2/+1
CFGBuilder::addStmt() implicitly passes AddStmtChoice::AlwaysAdd to Visit() already, so this should have no behavior change. Differential Revision: https://reviews.llvm.org/D111570
2021-10-11[clang] Convert a few loops to for-eachNico Weber1-5/+3
2021-10-11[Sema] Use llvm::is_contained (NFC)Kazu Hirata1-2/+1
2021-08-16[clang] Expose unreachable fallthrough annotation warningNathan Chancellor1-1/+1
The Linux kernel has a macro called IS_ENABLED(), which evaluates to a constant 1 or 0 based on Kconfig selections, allowing C code to be unconditionally enabled or disabled at build time. For example: int foo(struct *a, int b) { switch (b) { case 1: if (a->flag || !IS_ENABLED(CONFIG_64BIT)) return 1; __attribute__((fallthrough)); case 2: return 2; default: return 3; } } There is an unreachable warning about the fallthrough annotation in the first case because !IS_ENABLED(CONFIG_64BIT) can be evaluated to 1, which looks like return 1; __attribute__((fallthrough)); to clang. This type of warning is pointless for the Linux kernel because it does this trick all over the place due to the sheer number of configuration options that it has. Add -Wunreachable-code-fallthrough, enabled under -Wunreachable-code, so that projects that want to warn on unreachable code get this warning but projects that do not care about unreachable code can still use -Wimplicit-fallthrough without having to make changes to their code base. Fixes PR51094. Reviewed By: aaron.ballman, nickdesaulniers Differential Revision: https://reviews.llvm.org/D107933
2021-04-22[-Wcalled-once] Do not run analysis on Obj-C++Valeriy Savchenko1-1/+1
Objective-C++ is not yet suppoerted. rdar://76729552 Differential Revision: https://reviews.llvm.org/D100955
2021-03-18[-Wcalled-once-parameter] Harden analysis in terms of block useValeriy Savchenko1-48/+108
This patch introduces a very simple inter-procedural analysis between blocks and enclosing functions. We always analyze blocks first (analysis is done as part of semantic analysis that goes side-by-side with the parsing process), and at the moment of reporting we don't know how that block will be actually used. This patch introduces new logic delaying reports of the "never called" warnings on blocks. If we are not sure that the block will be called exactly once, we shouldn't warn our users about that. Double calls, however, don't require such delays. While analyzing the enclosing function, we can actually decide what we should do with those warnings. Additionally, as a side effect, we can be more confident about blocks in such context and can treat them not as escapes, but as direct calls. rdar://74090107 Differential Revision: https://reviews.llvm.org/D98688
2021-01-05[-Wcalled-once-parameter] Introduce 'called_once' attributeValeriy Savchenko1-0/+89
This commit introduces a new attribute `called_once`. It can be applied to function-like parameters to signify that this parameter should be called exactly once. This concept is particularly widespread in asynchronous programs. Additionally, this commit introduce a new group of dataflow analysis-based warnings to check this property. It identifies and reports the following situations: * parameter is called twice * parameter is never called * parameter is not called on one of the paths Current implementation can also automatically infer `called_once` attribute for completion handler paramaters that should follow the same principle by convention. This behavior is OFF by default and can be turned on by using `-Wcompletion-handler`. Differential Revision: https://reviews.llvm.org/D92039 rdar://72812043
2020-10-19Recommit "[CUDA][HIP] Defer overloading resolution diagnostics for host ↵Yaxun (Sam) Liu1-1/+1
device functions" This recommits 7f1f89ec8d9944559042bb6d3b1132eabe3409de and 40df06cdafc010002fc9cfe1dda73d689b7d27a6 with bug fixes for memory sanitizer failure and Tensile build failure.
2020-09-24Revert "Recommit "[CUDA][HIP] Defer overloading resolution diagnostics for ↵Reid Kleckner1-1/+1
host device functions"" This reverts commit e39da8ab6a286ac777d5fe7799f1eb782cf99938. This depends on a change that needs additional design review and needs to be reverted.
2020-09-24Recommit "[CUDA][HIP] Defer overloading resolution diagnostics for host ↵Yaxun (Sam) Liu1-1/+1
device functions" This recommits 7f1f89ec8d9944559042bb6d3b1132eabe3409de and 40df06cdafc010002fc9cfe1dda73d689b7d27a6 after fixing memory sanitizer failure.
2020-09-17Revert "[CUDA][HIP] Defer overloading resolution diagnostics for host device ↵Yaxun (Sam) Liu1-1/+1
functions" This reverts commit 7f1f89ec8d9944559042bb6d3b1132eabe3409de. This reverts commit 40df06cdafc010002fc9cfe1dda73d689b7d27a6.
2020-09-17[CUDA][HIP] Defer overloading resolution diagnostics for host device functionsYaxun (Sam) Liu1-1/+1
In CUDA/HIP a function may become implicit host device function by pragma or constexpr. A host device function is checked in both host and device compilation. However it may be emitted only on host or device side, therefore the diagnostics should be deferred until it is known to be emitted. Currently clang is only able to defer certain diagnostics. This causes false alarms and limits the usefulness of host device functions. This patch lets clang defer all overloading resolution diagnostics for host device functions. An option -fgpu-defer-diag is added to control this behavior. By default it is off. It is NFC for other languages. Differential Revision: https://reviews.llvm.org/D84364
2020-09-01Thread safety analysis: More consistent warning messageAaron Puchert1-0/+7
Other warning messages for negative capabilities also mention their kind, and the double space was ugly. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D84603
2020-08-05[clang][nearly-NFC] Remove some superfluous uses of NamedDecl::getNameAsStringBruno Ricci1-2/+2
`OS << ND->getDeclName();` is equivalent to `OS << ND->getNameAsString();` without the extra temporary string. This is not quite a NFC since two uses of `getNameAsString` in a diagnostic are replaced, which results in the named entity being quoted with additional "'"s (ie: 'var' instead of var).
2020-06-08Thread safety analysis: Add note for double unlockAaron Puchert1-3/+12
Summary: When getting a warning that we release a capability that isn't held it's sometimes not clear why. So just like we do for double locking, we add a note on the previous release operation, which marks the point since when the capability isn't held any longer. We can find this previous release operation by looking up the corresponding negative capability. Reviewers: aaron.ballman, delesley Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D81352
2020-06-02[Clang] Add a new warning to warn when passing uninitialized variables as ↵Zequan Wu1-5/+47
const reference parameters to a function Summary: Add a new warning -Wuninitialized-const-reference as a subgroup of -Wuninitialized to address a bug filed here: https://bugs.llvm.org/show_bug.cgi?id=45624 This warning is controlled by -Wuninitialized and can be disabled by -Wno-uninitialized-const-reference. The warning is diagnosed when passing uninitialized variables as const reference parameters to a function. Differential Revision: https://reviews.llvm.org/D79895
2020-03-02Revert "[clang] detect switch fallthrough marked by a comment (PR43465)"Luboš Luňák1-36/+0
This reverts commit 398b4ed87d488b42032c8d0304324dce76ba9b66. As requested in https://bugs.llvm.org/show_bug.cgi?id=43465#c37 .
2020-02-03[clang] detect switch fallthrough marked by a comment (PR43465)Luboš Luňák1-0/+36
The regex can be extended if needed, but this should probably handle most of the cases. Differential Revision: https://reviews.llvm.org/D73852
2019-12-17[Sema] Fixes -Wrange-loop-analysis warningsMark de Wever1-1/+1
This avoids new warnings due to D68912 adds -Wrange-loop-analysis to -Wall. Differential Revision: https://reviews.llvm.org/D71529
2019-10-19New tautological warning for bitwise-or with non-zero constant always true.Richard Trieu1-6/+18
Taking a value and the bitwise-or it with a non-zero constant will always result in a non-zero value. In a boolean context, this is always true. if (x | 0x4) {} // always true, intended '&' This patch creates a new warning group -Wtautological-bitwise-compare for this warning. It also moves in the existing tautological bitwise comparisons into this group. A few other changes were needed to the CFGBuilder so that all bool contexts would be checked. The warnings in -Wtautological-bitwise-compare will be off by default due to using the CFG. Fixes: https://bugs.llvm.org/show_bug.cgi?id=42666 Differential Revision: https://reviews.llvm.org/D66046 llvm-svn: 375318
2019-08-20[Attr] Support _attribute__ ((fallthrough))Nathan Huckleberry1-32/+26
Summary: Fixed extraneous matches of non-NullStmt Reviewers: aaron.ballman, rsmith, efriedma, xbolva00 Reviewed By: aaron.ballman, rsmith, xbolva00 Subscribers: riccibruno, arphaman, ziangwan, ojeda, xbolva00, nickdesaulniers, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64838 llvm-svn: 369414
2019-05-31Defer capture initialization for blocks until after we've left theRichard Smith1-5/+4
function scope. This removes one of the last few cases where we build expressions in the wrong function scope context. No functionality change intended. llvm-svn: 362178
2019-05-24[CFG] NFC: Remove implicit conversion from CFGTerminator to Stmt *.Artem Dergachev1-6/+8
Turn it into a variant class instead. This conversion does indeed save some code but there's a plan to add support for more kinds of terminators that aren't necessarily based on statements, and with those in mind it becomes more and more confusing to have CFGTerminators implicitly convertible to a Stmt *. Differential Revision: https://reviews.llvm.org/D61814 llvm-svn: 361586
2019-05-06Use DiagRuntimeBehavior for -Wunsequenced to weed out false positivesRichard Smith1-11/+13
where either the modification or the other access is unreachable. This reverts r359984 (which reverted r359962). The bug in clang-tidy's test suite exposed by the original commit was fixed in r360009. llvm-svn: 360010
2019-05-05Revert rL359962 : Use DiagRuntimeBehavior for -Wunsequenced to weed out ↵Simon Pilgrim1-13/+11
false positives where either the modification or the other access is unreachable. ........ Try to fix buildbots llvm-svn: 359984
2019-05-04Use DiagRuntimeBehavior for -Wunsequenced to weed out false positivesRichard Smith1-11/+13
where either the modification or the other access is unreachable. llvm-svn: 359962
2019-04-23Improve -Wuninitialized warning under ARC for block variables that areAkira Hatanaka1-1/+2
recursively captured. Under ARC, a block variable is zero-initialized when it is recursively captured by the block literal initializer. rdar://problem/11022762 llvm-svn: 359049
2019-03-19Minor renaming as suggested in review [NFC]Aaron Puchert1-11/+11
See D59455. llvm-svn: 356430
2019-03-18Thread safety analysis: Add note for unlock kind mismatchAaron Puchert1-14/+14
Summary: Similar to D56967, we add the existing diag::note_locked_here to tell the user where we saw the locking that isn't matched correctly. Reviewers: aaron.ballman, delesley Reviewed By: aaron.ballman Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D59455 llvm-svn: 356427
2019-02-13Restore Check for Unreachable Exit Block in -Winfinite-recursionRobert Widmann1-0/+4
Summary: When this was rewritten in D43737, the logic changed to better explore infinite loops. The check for a reachable exit block was deleted which accidentally introduced false positives in case the exit node was unreachable. We were testing for cases like this, but @steven_wu provided an additional test case that I've included in the regression tests for this patch. Reviewers: steven_wu, rtrieu Reviewed By: steven_wu, rtrieu Subscribers: cfe-commits, steven_wu Tags: #clang Differential Revision: https://reviews.llvm.org/D58122 llvm-svn: 353984
2019-01-29Thread safety analysis: Improve diagnostics for double lockingAaron Puchert1-14/+17
Summary: We use the existing diag::note_locked_here to tell the user where we saw the first locking. Reviewers: aaron.ballman, delesley Reviewed By: aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D56967 llvm-svn: 352549
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2019-01-14[AST] RecursiveASTVisitor visits lambda classes when implicit visitation is on.Sam McCall1-1/+6
Summary: This fixes ASTContext's parent map for nodes in such classes (e.g. operator()). https://bugs.llvm.org/show_bug.cgi?id=39949 This also changes the observed shape of the AST for implicit RAVs. - this includes AST MatchFinder: cxxRecordDecl() now matches lambda classes, functionDecl() matches the call operator, and the parent chain is body -> call operator -> lambda class -> lambdaexpr rather than body -> lambdaexpr. - this appears not to matter for the ASTImporterLookupTable builder - this doesn't matter for the other RAVs in-tree. In order to do this, we remove the TraverseLambdaBody hook. The problem is it's hard/weird to ensure this hook is called when traversing via the implicit class. There were just two users of this hook in-tree, who use it to skip bodies. I replaced these with explicitly traversing the captures only. Another approach would be recording the bodies when the lambda is visited, and then recognizing them later. I'd be open to suggestion on how to preserve this hook, instead. Reviewers: aaron.ballman, JonasToth Subscribers: cfe-commits, rsmith, jdennett Differential Revision: https://reviews.llvm.org/D56444 llvm-svn: 351047
2018-11-30Revert "Revert r347417 "Re-Reinstate 347294 with a fix for the failures.""Fangrui Song1-4/+3
It seems the two failing tests can be simply fixed after r348037 Fix 3 cases in Analysis/builtin-functions.cpp Delete the bad CodeGen/builtin-constant-p.c for now llvm-svn: 348053
2018-11-30Revert r347417 "Re-Reinstate 347294 with a fix for the failures."Fangrui Song1-3/+4
Kept the "indirect_builtin_constant_p" test case in test/SemaCXX/constant-expression-cxx1y.cpp while we are investigating why the following snippet fails: extern char extern_var; struct { int a; } a = {__builtin_constant_p(extern_var)}; llvm-svn: 348039
2018-11-28Re-commit r347417 "Re-Reinstate 347294 with a fix for the failures."Hans Wennborg1-4/+3
This was reverted in r347656 due to me thinking it caused a miscompile of Chromium. Turns out it was the Chromium code that was broken. llvm-svn: 347756
2018-11-27Revert r347417 "Re-Reinstate 347294 with a fix for the failures."Hans Wennborg1-3/+4
This caused a miscompile in Chrome (see crbug.com/908372) that's illustrated by this small reduction: static bool f(int *a, int *b) { return !__builtin_constant_p(b - a) || (!(b - a)); } int arr[] = {1,2,3}; bool g() { return f(arr, arr + 3); } $ clang -O2 -S -emit-llvm a.cc -o - g() should return true, but after r347417 it became false for some reason. This also reverts the follow-up commits. r347417: > Re-Reinstate 347294 with a fix for the failures. > > Don't try to emit a scalar expression for a non-scalar argument to > __builtin_constant_p(). > > Third time's a charm! r347446: > The result of is.constant() is unsigned. r347480: > A __builtin_constant_p() returns 0 with a function type. r347512: > isEvaluatable() implies a constant context. > > Assume that we're in a constant context if we're asking if the expression can > be compiled into a constant initializer. This fixes the issue where a > __builtin_constant_p() in a compound literal was diagnosed as not being > constant, even though it's always possible to convert the builtin into a > constant. r347531: > A "constexpr" is evaluated in a constant context. Make sure this is reflected > if a __builtin_constant_p() is a part of a constexpr. llvm-svn: 347656
2018-11-21Re-Reinstate 347294 with a fix for the failures.Bill Wendling1-4/+3
Don't try to emit a scalar expression for a non-scalar argument to __builtin_constant_p(). Third time's a charm! llvm-svn: 347417
2018-11-21Revert r347364 again, the fix was incomplete.Nico Weber1-3/+4
llvm-svn: 347389
2018-11-20Reinstate 347294 with a fix for the failures.Bill Wendling1-4/+3
EvaluateAsInt() is sometimes called in a constant context. When that's the case, we need to specify it as so. llvm-svn: 347364
2018-09-26llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)Fangrui Song1-1/+1
Summary: The convenience wrapper in STLExtras is available since rL342102. Reviewers: rsmith, #clang, dblaikie Reviewed By: rsmith, #clang Subscribers: mgrang, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D52576 llvm-svn: 343147