1 //=- AnalysisBasedWarnings.h - Sema warnings based on libAnalysis -*- C++ -*-=// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines AnalysisBasedWarnings, a worker object used by Sema 10 // that issues warnings based on dataflow-analysis. 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_SEMA_ANALYSISBASEDWARNINGS_H 14 #define LLVM_CLANG_SEMA_ANALYSISBASEDWARNINGS_H 15 16 #include "clang/AST/Decl.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include <memory> 19 20 namespace clang { 21 22 class Decl; 23 class FunctionDecl; 24 class QualType; 25 class Sema; 26 namespace sema { 27 class FunctionScopeInfo; 28 } 29 30 namespace sema { 31 32 class AnalysisBasedWarnings { 33 public: 34 class Policy { 35 friend class AnalysisBasedWarnings; 36 // The warnings to run. 37 LLVM_PREFERRED_TYPE(bool) 38 unsigned enableCheckFallThrough : 1; 39 LLVM_PREFERRED_TYPE(bool) 40 unsigned enableCheckUnreachable : 1; 41 LLVM_PREFERRED_TYPE(bool) 42 unsigned enableThreadSafetyAnalysis : 1; 43 LLVM_PREFERRED_TYPE(bool) 44 unsigned enableConsumedAnalysis : 1; 45 public: 46 Policy(); disableCheckFallThrough()47 void disableCheckFallThrough() { enableCheckFallThrough = 0; } 48 }; 49 50 private: 51 Sema &S; 52 Policy DefaultPolicy; 53 54 class InterProceduralData; 55 std::unique_ptr<InterProceduralData> IPData; 56 57 enum VisitFlag { NotVisited = 0, Visited = 1, Pending = 2 }; 58 llvm::DenseMap<const FunctionDecl*, VisitFlag> VisitedFD; 59 60 /// \name Statistics 61 /// @{ 62 63 /// Number of function CFGs built and analyzed. 64 unsigned NumFunctionsAnalyzed; 65 66 /// Number of functions for which the CFG could not be successfully 67 /// built. 68 unsigned NumFunctionsWithBadCFGs; 69 70 /// Total number of blocks across all CFGs. 71 unsigned NumCFGBlocks; 72 73 /// Largest number of CFG blocks for a single function analyzed. 74 unsigned MaxCFGBlocksPerFunction; 75 76 /// Total number of CFGs with variables analyzed for uninitialized 77 /// uses. 78 unsigned NumUninitAnalysisFunctions; 79 80 /// Total number of variables analyzed for uninitialized uses. 81 unsigned NumUninitAnalysisVariables; 82 83 /// Max number of variables analyzed for uninitialized uses in a single 84 /// function. 85 unsigned MaxUninitAnalysisVariablesPerFunction; 86 87 /// Total number of block visits during uninitialized use analysis. 88 unsigned NumUninitAnalysisBlockVisits; 89 90 /// Max number of block visits during uninitialized use analysis of 91 /// a single function. 92 unsigned MaxUninitAnalysisBlockVisitsPerFunction; 93 94 /// @} 95 96 public: 97 AnalysisBasedWarnings(Sema &s); 98 ~AnalysisBasedWarnings(); 99 100 void IssueWarnings(Policy P, FunctionScopeInfo *fscope, 101 const Decl *D, QualType BlockType); 102 103 // Issue warnings that require whole-translation-unit analysis. 104 void IssueWarnings(TranslationUnitDecl *D); 105 getDefaultPolicy()106 Policy getDefaultPolicy() { return DefaultPolicy; } 107 108 void PrintStats() const; 109 }; 110 111 } // namespace sema 112 } // namespace clang 113 114 #endif 115