This project contains clang's experimental implementation of the lifetime safety profile of the C++ core guidelines.
You can try this implementation on Compiler Explorer
by choosing the clang version experimental -Wlifetime
.
-Wdangling-gsl
: Statement-local analysis (enabled by default)-Wreturn-stack-address
: Statement-local analysis (enabled by default)-Wlifetime
: Main flow-sensitive analysis-Wlifetime-filter
: Reduce the number of false-postives at the cost of additional false negatives.-Wno-lifetime-null
: Disable flow-sensitive warnings about nullness of Pointers -Wlifetime-global
: Flow-sensitive analysis that requires global variables of Pointer type to always point to variables with static lifetime-Wlifetime-disabled
: Get warnings when the flow-sensitive analysis is disabled on a function due to forbidden constructs (reinterpret_cast
or pointer arithmetic)Suppress Warnings
[[gsl::suppress("lifetime")]]
attribute can be used to suppress lifetime warnings for functions. See clang/test/Sema/attr-lifetime-suppress.cpp for an example.You can either try it on Compiler Explorer, or build clang yourself:
mkdir build
cd build
cmake ../llvm -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS=clang
make
# Run the tests (optional)
make check-clang
# Use clang
./bin/clang++ <some-source> <warning-flags>
CppCon 2019 Talk, focusing on statement local analysis
EuroLLVM 2019 Talk, focusing on flow-sensitive analysis
CppCon 2018 Talk, focusing on flow-sensitive analysis
CppCon 2018 Keynote, by Herb Sutter
CppCon 2015 Keynote, by Herb Sutter, first introduction
Tests:
-Wlifetime
-Wno-lifetime-null
Implementation:
runAnalysis
: https://github.com/mgehre/llvm-project/blob/lifetime/clang/lib/Sema/AnalysisBasedWarnings.cpp#L2277runAnalysis
https://github.com/mgehre/llvm-project/blob/lifetime/clang/lib/Analysis/Lifetime.cpp#L294, delegating to LifetimeContext
. VisitBlock
https://github.com/mgehre/llvm-project/blob/lifetime/clang/lib/Analysis/Lifetime.cpp#L253 to compute the psets of a BasicBlock. VisitBlock
https://github.com/mgehre/llvm-project/blob/lifetime/clang/lib/Analysis/LifetimePsetBuilder.cpp#L1255 uses a PSetsBuilder
to visit each (sub-)expression in basic block. Visit*
member functions, e.g. https://github.com/mgehre/llvm-project/blob/lifetime/clang/lib/Analysis/LifetimePsetBuilder.cpp#L1719, to compute the pset of the current expression (if it has one - i.e. it's an lvalue or an prvalue of Pointer type) based on the psets of its subexpressions (which have been computed earlier).