llvm-project/clang/lib/Checker
John McCall 717d9b0e2f It's kindof silly that ExtQuals has an ASTContext&, and we can use that
space better.  Remove this reference.  To make that work, change some APIs
(most importantly, getDesugaredType()) to take an ASTContext& if they
need to return a QualType.  Simultaneously, diminish the need to return a
QualType by introducing some useful APIs on SplitQualType, which is
just a std::pair<const Type *, Qualifiers>.

llvm-svn: 121478
2010-12-10 11:01:00 +00:00
..
AdjustedReturnValueChecker.cpp Rename all 'EvalXXX' methods in libChecker to 2010-12-01 21:57:22 +00:00
AggExprVisitor.cpp Let StackFrameContext represent if the call expr is evaluated as lvalue. 2010-11-24 13:08:51 +00:00
AnalysisConsumer.cpp Remove leftover code and use the text path diagnostic client even without a specified output file since 2010-12-03 01:17:19 +00:00
AnalysisManager.cpp Use StackFrameContext directly in CallEnter program point. Then we don't need 2010-11-24 08:53:20 +00:00
AnalyzerStatsChecker.cpp Make sure to always check the result of 2010-11-12 07:15:47 +00:00
ArrayBoundChecker.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
AttrNonNullChecker.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
BasicConstraintManager.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
BasicObjCFoundationChecks.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
BasicObjCFoundationChecks.h Convert GRSimpleAPIChecks in BasicObjCFoundationChecks to be Checkers. 2010-10-20 23:38:56 +00:00
BasicStore.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
BasicValueFactory.cpp Rename all 'EvalXXX' methods in libChecker to 2010-12-01 21:57:22 +00:00
BugReporter.cpp Minor refactoring; have BugReport::getRanges return a pair of iterator, no functionality change. 2010-12-04 01:12:15 +00:00
BugReporterVisitors.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
BuiltinFunctionChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
CFRefCount.cpp Minor refactoring; have BugReport::getRanges return a pair of iterator, no functionality change. 2010-12-04 01:12:15 +00:00
CMakeLists.txt Introduce TextPathDiagnostics, a simple PathDiagnosticClient that outputs as diagnostic notes 2010-12-03 00:58:14 +00:00
CStringChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
CallAndMessageChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
CastSizeChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
CastToStructChecker.cpp Introduce Type::isStructureOrClassType(), which does the obvious 2010-04-26 21:31:17 +00:00
CheckDeadStores.cpp Rename CXXExprWithTemporaries -> ExprWithCleanups; there's no theoretical 2010-12-06 08:20:24 +00:00
CheckObjCDealloc.cpp Simplify the ASTs by consolidating ObjCImplicitGetterSetterExpr and ObjCPropertyRefExpr 2010-12-02 01:19:52 +00:00
CheckObjCInstMethSignature.cpp Add raw_ostream operators to NamedDecl for convenience. Switch over all users of getNameAsString on a stream. 2010-04-17 09:33:03 +00:00
CheckSecuritySyntaxOnly.cpp Although we currently have explicit lvalue-to-rvalue conversions, they're 2010-12-04 03:47:34 +00:00
CheckSizeofPointer.cpp Move 'LocalCheckers.h' to the 'Checkers' subdirectory. 2010-01-26 22:59:55 +00:00
Checker.cpp
CheckerHelpers.cpp Zap unused UnaryOperator::OffsetOf. 2010-08-13 01:36:11 +00:00
ChrootChecker.cpp Rename all 'EvalXXX' methods in libChecker to 2010-12-01 21:57:22 +00:00
CocoaConventions.cpp Correctly handle 'Class<...>' when examining Cocoa conventions in the static analyzer. Fixes a crash reported in <rdar://problem/8272168>. Patch by Henry Mason! 2010-08-05 00:19:24 +00:00
DereferenceChecker.cpp Although we currently have explicit lvalue-to-rvalue conversions, they're 2010-12-04 03:47:34 +00:00
DivZeroChecker.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
Environment.cpp Rename CXXExprWithTemporaries -> ExprWithCleanups; there's no theoretical 2010-12-06 08:20:24 +00:00
ExplodedGraph.cpp
FixedAddressChecker.cpp GCC didn't care for my attempt at API compatibility, so brute-force everything 2010-08-25 11:45:40 +00:00
FlatStore.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
FrontendActions.cpp Break Frontend's dependency on Rewrite, Checker and CodeGen in shared library configuration 2010-06-15 17:48:49 +00:00
GRBlockCounter.cpp Adjust method calls to reflect name changes in 2010-11-24 00:54:37 +00:00
GRCXXExprEngine.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
GRCoreEngine.cpp Let StackFrameContext represent if the call expr is evaluated as lvalue. 2010-11-24 13:08:51 +00:00
GRExprEngine.cpp It's kindof silly that ExtQuals has an ASTContext&, and we can use that 2010-12-10 11:01:00 +00:00
GRExprEngineExperimentalChecks.cpp Add experimental chroot check which checks improper use of chroot(). Patch by 2010-10-10 05:45:30 +00:00
GRExprEngineExperimentalChecks.h Add experimental chroot check which checks improper use of chroot(). Patch by 2010-10-10 05:45:30 +00:00
GRExprEngineInternalChecks.h Implement: <rdar://problem/6351970> rule request: warn if @synchronized mutex can be nil 2010-09-10 03:05:40 +00:00
GRState.cpp Mark SVal constructors 'explicit'. 2010-12-05 23:36:20 +00:00
HTMLDiagnostics.cpp Rename Create[*]DiagnosticClient -> create[*]DiagnosticClient. 2010-12-03 00:58:08 +00:00
IdempotentOperationChecker.cpp Remove the TypesCompatibleExprClass AST node. Merge its functionality into BinaryTypeTraitExpr. 2010-12-08 22:35:30 +00:00
LLVMConventionsChecker.cpp Rename CXXExprWithTemporaries -> ExprWithCleanups; there's no theoretical 2010-12-06 08:20:24 +00:00
MacOSXAPIChecker.cpp Checker: random include cleanup. 2010-03-27 21:19:47 +00:00
Makefile BUILD_ARCHIVE is the default for libraries, no need to set it. 2010-07-18 00:14:47 +00:00
MallocChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
ManagerRegistry.cpp
MemRegion.cpp It's kindof silly that ExtQuals has an ASTContext&, and we can use that 2010-12-10 11:01:00 +00:00
NSAutoreleasePoolChecker.cpp Overhaul the AST representation of Objective-C message send 2010-04-21 00:45:42 +00:00
NSErrorChecker.cpp Add raw_ostream operators to NamedDecl for convenience. Switch over all users of getNameAsString on a stream. 2010-04-17 09:33:03 +00:00
NoReturnFunctionChecker.cpp the big refactoring bits of PR3782. 2010-03-30 20:24:48 +00:00
OSAtomicChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
ObjCAtSyncChecker.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
ObjCUnusedIVarsChecker.cpp Don't warn about unamed bitfield ivars in the ObjCUnusedIvarsChecker. Fixes <rdar://problem/8481311>. 2010-10-28 02:16:22 +00:00
PathDiagnostic.cpp Since multiple diagnostics can share one diagnostic client, have the client keeping track 2010-11-18 20:06:46 +00:00
PlistDiagnostics.cpp Rename Create[*]DiagnosticClient -> create[*]DiagnosticClient. 2010-12-03 00:58:08 +00:00
PointerArithChecker.cpp GCC didn't care for my attempt at API compatibility, so brute-force everything 2010-08-25 11:45:40 +00:00
PointerSubChecker.cpp GCC didn't care for my attempt at API compatibility, so brute-force everything 2010-08-25 11:45:40 +00:00
PthreadLockChecker.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
README.txt Adds a README for the Clang static analyzer to document its design and work flow. The contents are taken from email notes by kremenek and xuzhongxing. 2010-11-15 23:19:25 +00:00
RangeConstraintManager.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
RegionStore.cpp It's kindof silly that ExtQuals has an ASTContext&, and we can use that 2010-12-10 11:01:00 +00:00
ReturnPointerRangeChecker.cpp Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
ReturnUndefChecker.cpp Add range of return value expression in ReturnUndefChecker. Patch by Jim Goodnow II! 2010-08-31 01:35:32 +00:00
SValBuilder.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
SVals.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
SimpleConstraintManager.cpp PR5207: Change APInt methods trunc(), sext(), zext(), sextOrTrunc() and 2010-12-07 08:25:34 +00:00
SimpleConstraintManager.h Rename all 'AssumeXXX' methods in libChecker 2010-12-01 22:16:56 +00:00
SimpleSValBuilder.cpp PR5207: Change APInt methods trunc(), sext(), zext(), sextOrTrunc() and 2010-12-07 08:25:34 +00:00
StackAddrLeakChecker.cpp Rename all 'EvalXXX' methods in libChecker to 2010-12-01 21:57:22 +00:00
Store.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
StreamChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
SymbolManager.cpp Enabled relaxed LiveVariables analysis in the path-sensitive engine to increase the coverage of bugs. Primarily affects IdempotentOperationChecker. 2010-08-27 22:46:32 +00:00
TextPathDiagnostics.cpp Also show notes about the taken path in TextPathDiagnostics. 2010-12-03 02:03:26 +00:00
UndefBranchChecker.cpp Constify all references to Stmt* and CFGBlock* in libChecker. 2010-07-20 06:22:24 +00:00
UndefCapturedBlockVarChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
UndefResultChecker.cpp Checker: random include cleanup. 2010-03-27 21:19:47 +00:00
UndefinedArraySubscriptChecker.cpp Checker: random include cleanup. 2010-03-27 21:19:47 +00:00
UndefinedAssignmentChecker.cpp For GRExprEngine::EvalBind() (and called visitors), unifiy StoreE and AssignE. Now StoreE (const Stmt*) represents the expression where the store took place, which is the assignment expression if it takes place in an assignment. This removes some conceptual dissidence as well as removes an extra parameter from the Checker::PreVisitBind() visitor. It also improves ranges and source location information in analyzer diagnostics. 2010-09-02 00:56:20 +00:00
UnixAPIChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00
UnreachableCodeChecker.cpp UnreachableCodeChecker cleanup and improvements 2010-10-06 23:02:25 +00:00
VLASizeChecker.cpp Merge ValueManager into SValBuilder. 2010-12-02 07:49:45 +00:00

README.txt

//===----------------------------------------------------------------------===//
// Clang Static Analyzer
//===----------------------------------------------------------------------===//

= Library Structure =

The analyzer library has two layers: a (low-level) static analysis
engine (GRExprEngine.cpp and friends), and some static checkers
(*Checker.cpp).  The latter are built on top of the former via the
Checker and CheckerVisitor interfaces (Checker.h and
CheckerVisitor.h).  The Checker interface is designed to be minimal
and simple for checker writers, and attempts to isolate them from much
of the gore of the internal analysis engine.

= How It Works =

The analyzer is inspired by several foundational research papers ([1],
[2]).  (FIXME: kremenek to add more links)

In a nutshell, the analyzer is basically a source code simulator that
traces out possible paths of execution.  The state of the program
(values of variables and expressions) is encapsulated by the state
(GRState).  A location in the program is called a program point
(ProgramPoint), and the combination of state and program point is a
node in an exploded graph (ExplodedGraph).  The term "exploded" comes
from exploding the control-flow edges in the control-flow graph (CFG).

Conceptually the analyzer does a reachability analysis through the
ExplodedGraph.  We start at a root node, which has the entry program
point and initial state, and then simulate transitions by analyzing
individual expressions.  The analysis of an expression can cause the
state to change, resulting in a new node in the ExplodedGraph with an
updated program point and an updated state.  A bug is found by hitting
a node that satisfies some "bug condition" (basically a violation of a
checking invariant).

The analyzer traces out multiple paths by reasoning about branches and
then bifurcating the state: on the true branch the conditions of the
branch are assumed to be true and on the false branch the conditions
of the branch are assumed to be false.  Such "assumptions" create
constraints on the values of the program, and those constraints are
recorded in the GRState object (and are manipulated by the
ConstraintManager).  If assuming the conditions of a branch would
cause the constraints to be unsatisfiable, the branch is considered
infeasible and that path is not taken.  This is how we get
path-sensitivity.  We reduce exponential blow-up by caching nodes.  If
a new node with the same state and program point as an existing node
would get generated, the path "caches out" and we simply reuse the
existing node.  Thus the ExplodedGraph is not a DAG; it can contain
cycles as paths loop back onto each other and cache out.

GRState and ExplodedNodes are basically immutable once created.  Once
one creates a GRState, you need to create a new one to get a new
GRState.  This immutability is key since the ExplodedGraph represents
the behavior of the analyzed program from the entry point.  To
represent these efficiently, we use functional data structures (e.g.,
ImmutableMaps) which share data between instances.

Finally, individual Checkers work by also manipulating the analysis
state.  The analyzer engine talks to them via a visitor interface.
For example, the PreVisitCallExpr() method is called by GRExprEngine
to tell the Checker that we are about to analyze a CallExpr, and the
checker is asked to check for any preconditions that might not be
satisfied.  The checker can do nothing, or it can generate a new
GRState and ExplodedNode which contains updated checker state.  If it
finds a bug, it can tell the BugReporter object about the bug,
providing it an ExplodedNode which is the last node in the path that
triggered the problem.

= Working on the Analyzer =

If you are interested in bringing up support for C++ expressions, the
best place to look is the visitation logic in GRExprEngine, which
handles the simulation of individual expressions.  There are plenty of
examples there of how other expressions are handled.

If you are interested in writing checkers, look at the Checker and
CheckerVisitor interfaces (Checker.h and CheckerVisitor.h).  Also look
at the files named *Checker.cpp for examples on how you can implement
these interfaces.

= Debugging the Analyzer =

There are some useful command-line options for debugging.  For example:

$ clang -cc1 -help | grep analyze
 -analyze-function <value>
 -analyzer-display-progress
 -analyzer-viz-egraph-graphviz
 ...

The first allows you to specify only analyzing a specific function.
The second prints to the console what function is being analyzed.  The
third generates a graphviz dot file of the ExplodedGraph.  This is
extremely useful when debugging the analyzer and viewing the
simulation results.

Of course, viewing the CFG (Control-Flow Graph) is also useful:

$ clang -cc1 -help | grep cfg
 -cfg-add-implicit-dtors Add C++ implicit destructors to CFGs for all analyses
 -cfg-add-initializers   Add C++ initializers to CFGs for all analyses
 -cfg-dump               Display Control-Flow Graphs
 -cfg-view               View Control-Flow Graphs using GraphViz
 -unoptimized-cfg        Generate unoptimized CFGs for all analyses

-cfg-dump dumps a textual representation of the CFG to the console,
and -cfg-view creates a GraphViz representation.

= References =

[1] Precise interprocedural dataflow analysis via graph reachability,
    T Reps, S Horwitz, and M Sagiv, POPL '95,
    http://portal.acm.org/citation.cfm?id=199462

[2] A memory model for static analysis of C programs, Z Xu, T
    Kremenek, and J Zhang, http://lcs.ios.ac.cn/~xzx/memmodel.pdf