19 Commits

Author SHA1 Message Date
David Stone
1eea63811a
[clang][NFC] Use constructor instead of factory function in CFGStmtMap (#172530)
`CFGStmtMap::Build` accepts pointers and returns a pointer to
dynamically allocated memory. In the one location where the type is
actually constructed, the pointers are guaranteed to be non-null. By
accepting references to statically enforce this, we can remove the only
way for the construction to fail.

By making this change, we also allow our user to decide how they want to
own the memory (either directly or indirectly). The user does not
actually need dynamic allocation here, so we replace the
`std::unique_ptr` with `std::optional`.

This simplifies the code by requiring fewer checks, makes comments on
what happens redundant because the code can obviously do only one thing,
avoids potential bugs, and improves performance by allocating less.
2025-12-24 20:25:00 +00:00
David Stone
cdac67d957
[clang][NFC] Make CFGStmtMap const-correct (#172529) 2025-12-24 19:41:41 +00:00
David Stone
1ef7348682
[clang][NFC] In CFGStmtMap, do not use a void * data member, just use the object directly (#172528)
There is no reason to dynamically allocate `llvm::DenseMap` and try to
hide the type. A header we include anyway already includes `DenseMap.h`
so we save almost no compilation time. This change improves performance
by avoiding the dynamic allocation, and simplifies the code
considerably.

Now that we just have a regular data member, there is also no need for a
manual destructor, and the copy / move operations will do the right
thing.

In `getBlock`, we have some code that a comment claims is implementing
memoization, but in reality it does nothing. The relevant expression is
a conditional `(*SM)[X] = B`, but `B` is equal to `SM->find(X)->second`.

In `Accumulate`, we have a bunch of code to add things to the map for
the initial set-up. However, the original code would either find or
default construct an element, and then if the found element is equal to
the default constructed element it would set it to `B`. Rather than
doing this in two steps, we can simply use `try_emplace` to insert if
it's not already present. This change is sound only if the new element
we are inserting cannot be equal to the default constructed element, but
the element type is a pointer and this entire section of code assumes
`B` is not null.
2025-12-24 12:08:25 -07:00
David Stone
ba05883304
[clang][NFC] In CFGStmtMap, remove mutable getBlock overload. (#172364)
The mutable version of the overload is not used. The way we implemented
code sharing in the const vs. mutable overloads had a const-correctness
violation, anyway.
2025-12-16 10:14:57 -07:00
Kazu Hirata
eb07f0d4a9
[Analysis] Use range-based for loops (NFC) (#146466) 2025-07-01 19:38:28 -07:00
Kazu Hirata
6ad0788c33 [clang] Use std::optional instead of llvm::Optional (NFC)
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 12:31:01 -08:00
Kazu Hirata
a1580d7b59 [clang] Add #include <optional> (NFC)
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-14 11:07:21 -08:00
Artem Dergachev
4e53032d9b [CFG] NFC: Remove implicit conversion from CFGTerminator to Stmt *.
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-24 01:34:22 +00:00
Chandler Carruth
2946cd7010 Update the file headers across all of the LLVM projects in the monorepo
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-19 08:50:56 +00:00
Fangrui Song
6907ce2f8f Remove trailing space
sed -Ei 's/[[:space:]]+$//' include/**/*.{def,h,td} lib/**/*.{cpp,h}

llvm-svn: 338291
2018-07-30 19:24:48 +00:00
Craig Topper
2554294321 [C++11] Use 'nullptr'. Analysis edition.
llvm-svn: 209191
2014-05-20 04:30:07 +00:00
David Blaikie
00be69ab5c Remove the CFGElement "Invalid" state.
Use Optional<CFG*> where invalid states were needed previously. In the one case
where that's not possible (beginAutomaticObjDtorsInsert) just use a dummy
CFGAutomaticObjDtor.

Thanks for the help from Jordan Rose & discussion/feedback from Ted Kremenek
and Doug Gregor.

Post commit code review feedback on r175796 by Ted Kremenek.

llvm-svn: 175938
2013-02-23 00:29:34 +00:00
David Blaikie
2a01f5d426 Replace CFGElement llvm::cast support to be well-defined.
See r175462 for another example/more details.

llvm-svn: 175796
2013-02-21 20:58:29 +00:00
Ted Kremenek
adfb445466 Constify the result of CFGStmt::getStmt().
llvm-svn: 138408
2011-08-23 23:05:04 +00:00
Ted Kremenek
96a7a59119 In preparation for fixing PR 6884, rework CFGElement to have getAs<> return pointers instead of fresh CFGElements.
- Also, consoldiate getDtorKind() and getKind() into one "kind".
- Add empty getDestructorDecl() method to CFGImplicitDtor.

llvm-svn: 126738
2011-03-01 03:15:10 +00:00
Zhongxing Xu
2cd7a78c76 Introduce new CFGElement hierarchy to support C++ CFG, based on Marcin's patch
and discussions with Ted and Jordy.

llvm-svn: 114056
2010-09-16 01:25:47 +00:00
Tom Care
ec389da6f2 Fix a bug where child statements could not be identified as being in a CFGBlock in CFGStmtMap::getBlock.
llvm-svn: 110881
2010-08-11 23:36:58 +00:00
Ted Kremenek
8a297e9f27 Remove bonehead redeclaration.
llvm-svn: 110288
2010-08-05 00:03:46 +00:00
Ted Kremenek
0950212b50 Add CFGStmtMap, which defines a mapping from Stmt* to CFGBlock*. The immediate intended use is in the unreachable code analysis.
llvm-svn: 110230
2010-08-04 18:23:15 +00:00