From: Reid Kleckner Date: Mon, 12 Aug 2013 23:49:39 +0000 (+0000) Subject: Remove Sema includes from Analysis code to fix layering X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d84f6b563e39c1e90e4d3d7e6846d46bc58ff5d;p=clang Remove Sema includes from Analysis code to fix layering This moves a header-only class from Sema to Analysis and puts the option check in Sema. Patch by Chris Wailes! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@188230 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Analysis/Analyses/Consumed.h b/include/clang/Analysis/Analyses/Consumed.h index 2800b01d66..8831fccc88 100644 --- a/include/clang/Analysis/Analyses/Consumed.h +++ b/include/clang/Analysis/Analyses/Consumed.h @@ -21,12 +21,79 @@ #include "clang/Analysis/AnalysisContext.h" #include "clang/Analysis/Analyses/PostOrderCFGView.h" #include "clang/Basic/SourceLocation.h" -#include "clang/Sema/ConsumedWarningsHandler.h" -#include "clang/Sema/Sema.h" namespace clang { namespace consumed { - + + typedef SmallVector OptionalNotes; + typedef std::pair DelayedDiag; + typedef std::list DiagList; + + class ConsumedWarningsHandlerBase { + + public: + + virtual ~ConsumedWarningsHandlerBase(); + + /// \brief Emit the warnings and notes left by the analysis. + virtual void emitDiagnostics() {} + + /// Warn about unnecessary-test errors. + /// \param VariableName -- The name of the variable that holds the unique + /// value. + /// + /// \param Loc -- The SourceLocation of the unnecessary test. + virtual void warnUnnecessaryTest(StringRef VariableName, + StringRef VariableState, + SourceLocation Loc) {} + + /// Warn about use-while-consumed errors. + /// \param MethodName -- The name of the method that was incorrectly + /// invoked. + /// + /// \param VariableName -- The name of the variable that holds the unique + /// value. + /// + /// \param Loc -- The SourceLocation of the method invocation. + virtual void warnUseOfTempWhileConsumed(StringRef MethodName, + SourceLocation Loc) {} + + /// Warn about use-in-unknown-state errors. + /// \param MethodName -- The name of the method that was incorrectly + /// invoked. + /// + /// \param VariableName -- The name of the variable that holds the unique + /// value. + /// + /// \param Loc -- The SourceLocation of the method invocation. + virtual void warnUseOfTempInUnknownState(StringRef MethodName, + SourceLocation Loc) {} + + /// Warn about use-while-consumed errors. + /// \param MethodName -- The name of the method that was incorrectly + /// invoked. + /// + /// \param VariableName -- The name of the variable that holds the unique + /// value. + /// + /// \param Loc -- The SourceLocation of the method invocation. + virtual void warnUseWhileConsumed(StringRef MethodName, + StringRef VariableName, + SourceLocation Loc) {} + + /// Warn about use-in-unknown-state errors. + /// \param MethodName -- The name of the method that was incorrectly + /// invoked. + /// + /// \param VariableName -- The name of the variable that holds the unique + /// value. + /// + /// \param Loc -- The SourceLocation of the method invocation. + virtual void warnUseInUnknownState(StringRef MethodName, + StringRef VariableName, + SourceLocation Loc) {} + }; + enum ConsumedState { // No state information for the given variable. CS_None, @@ -99,8 +166,6 @@ namespace consumed { typedef llvm::DenseMap CacheMapType; typedef std::pair CachePairType; - Sema &S; - ConsumedBlockInfo BlockInfo; ConsumedStateMap *CurrStates; @@ -112,13 +177,10 @@ namespace consumed { public: ConsumedWarningsHandlerBase &WarningsHandler; - - ConsumedAnalyzer(Sema &S, ConsumedWarningsHandlerBase &WarningsHandler) - : S(S), WarningsHandler(WarningsHandler) {} - - /// \brief Get a constant reference to the Sema object. - const Sema & getSema(void); - + + ConsumedAnalyzer(ConsumedWarningsHandlerBase &WarningsHandler) + : WarningsHandler(WarningsHandler) {} + /// \brief Check to see if the type is a consumable type. bool isConsumableType(QualType Type); @@ -131,7 +193,6 @@ namespace consumed { void run(AnalysisDeclContext &AC); }; - unsigned checkEnabled(DiagnosticsEngine &D); /// \brief Check to see if a function tests an object's validity. bool isTestingFunction(const CXXMethodDecl *MethodDecl); diff --git a/include/clang/Sema/ConsumedWarningsHandler.h b/include/clang/Sema/ConsumedWarningsHandler.h deleted file mode 100644 index 392d781e9e..0000000000 --- a/include/clang/Sema/ConsumedWarningsHandler.h +++ /dev/null @@ -1,98 +0,0 @@ -//===- ConsumedWarningsHandler.h -------------------------------*- C++ --*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// A handler class for warnings issued by the consumed analysis. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CLANG_CONSUMED_WARNING_HANDLER_H -#define LLVM_CLANG_CONSUMED_WARNING_HANDLER_H - -#include -#include - -#include "clang/Basic/SourceLocation.h" -#include "clang/Sema/Sema.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringRef.h" - -namespace clang { -namespace consumed { - - typedef SmallVector OptionalNotes; - typedef std::pair DelayedDiag; - typedef std::list DiagList; - - class ConsumedWarningsHandlerBase { - - public: - - virtual ~ConsumedWarningsHandlerBase(); - - /// \brief Emit the warnings and notes left by the analysis. - virtual void emitDiagnostics() {} - - /// Warn about unnecessary-test errors. - /// \param VariableName -- The name of the variable that holds the unique - /// value. - /// - /// \param Loc -- The SourceLocation of the unnecessary test. - virtual void warnUnnecessaryTest(StringRef VariableName, - StringRef VariableState, - SourceLocation Loc) {} - - /// Warn about use-while-consumed errors. - /// \param MethodName -- The name of the method that was incorrectly - /// invoked. - /// - /// \param VariableName -- The name of the variable that holds the unique - /// value. - /// - /// \param Loc -- The SourceLocation of the method invocation. - virtual void warnUseOfTempWhileConsumed(StringRef MethodName, - SourceLocation Loc) {} - - /// Warn about use-in-unknown-state errors. - /// \param MethodName -- The name of the method that was incorrectly - /// invoked. - /// - /// \param VariableName -- The name of the variable that holds the unique - /// value. - /// - /// \param Loc -- The SourceLocation of the method invocation. - virtual void warnUseOfTempInUnknownState(StringRef MethodName, - SourceLocation Loc) {} - - /// Warn about use-while-consumed errors. - /// \param MethodName -- The name of the method that was incorrectly - /// invoked. - /// - /// \param VariableName -- The name of the variable that holds the unique - /// value. - /// - /// \param Loc -- The SourceLocation of the method invocation. - virtual void warnUseWhileConsumed(StringRef MethodName, - StringRef VariableName, - SourceLocation Loc) {} - - /// Warn about use-in-unknown-state errors. - /// \param MethodName -- The name of the method that was incorrectly - /// invoked. - /// - /// \param VariableName -- The name of the variable that holds the unique - /// value. - /// - /// \param Loc -- The SourceLocation of the method invocation. - virtual void warnUseInUnknownState(StringRef MethodName, - StringRef VariableName, - SourceLocation Loc) {} - }; -}} // end clang::consumed - -#endif diff --git a/lib/Analysis/Consumed.cpp b/lib/Analysis/Consumed.cpp index 5995544be6..e556a8cef5 100644 --- a/lib/Analysis/Consumed.cpp +++ b/lib/Analysis/Consumed.cpp @@ -26,8 +26,6 @@ #include "clang/Analysis/Analyses/Consumed.h" #include "clang/Basic/OperatorKinds.h" #include "clang/Basic/SourceLocation.h" -#include "clang/Sema/ConsumedWarningsHandler.h" -#include "clang/Sema/SemaDiagnostic.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/raw_ostream.h" @@ -100,7 +98,8 @@ class ConsumedStmtVisitor : public ConstStmtVisitor { typedef llvm::DenseMap MapType; typedef std::pair PairType; typedef MapType::iterator InfoEntry; - + + AnalysisDeclContext ∾ ConsumedAnalyzer &Analyzer; ConsumedStateMap *StateMap; MapType PropagationMap; @@ -124,10 +123,11 @@ public: void VisitMemberExpr(const MemberExpr *MExpr); void VisitUnaryOperator(const UnaryOperator *UOp); void VisitVarDecl(const VarDecl *Var); - - ConsumedStmtVisitor(ConsumedAnalyzer &Analyzer, ConsumedStateMap *StateMap) : - Analyzer(Analyzer), StateMap(StateMap) {} - + + ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer, + ConsumedStateMap *StateMap) + : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {} + void reset() { PropagationMap.clear(); } @@ -227,8 +227,8 @@ void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) { void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) { CXXConstructorDecl *Constructor = Call->getConstructor(); - - ASTContext &CurrContext = Analyzer.getSema().getASTContext(); + + ASTContext &CurrContext = AC.getASTContext(); QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType(); if (Analyzer.isConsumableType(ThisType)) { @@ -636,10 +636,6 @@ void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) { Map[Var] = State; } -const Sema & ConsumedAnalyzer::getSema() { - return S; -} - bool ConsumedAnalyzer::isConsumableType(QualType Type) { const CXXRecordDecl *RD = @@ -737,9 +733,9 @@ void ConsumedAnalyzer::run(AnalysisDeclContext &AC) { if (CurrStates == NULL) CurrStates = BlockInfo.getInfo(CurrBlock); - - ConsumedStmtVisitor Visitor(*this, CurrStates); - + + ConsumedStmtVisitor Visitor(AC, *this, CurrStates); + // Visit all of the basic block's statements. for (CFGBlock::const_iterator BI = CurrBlock->begin(), BE = CurrBlock->end(); BI != BE; ++BI) { @@ -789,12 +785,6 @@ void ConsumedAnalyzer::run(AnalysisDeclContext &AC) { WarningsHandler.emitDiagnostics(); } -unsigned checkEnabled(DiagnosticsEngine &D) { - return (unsigned) - (D.getDiagnosticLevel(diag::warn_use_while_consumed, SourceLocation()) != - DiagnosticsEngine::Ignored); -} - bool isTestingFunction(const CXXMethodDecl *Method) { return Method->hasAttr(); } diff --git a/lib/Sema/AnalysisBasedWarnings.cpp b/lib/Sema/AnalysisBasedWarnings.cpp index 19fc29b907..54b791e6f6 100644 --- a/lib/Sema/AnalysisBasedWarnings.cpp +++ b/lib/Sema/AnalysisBasedWarnings.cpp @@ -1557,8 +1557,10 @@ clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) DefaultPolicy.enableThreadSafetyAnalysis = (unsigned) (D.getDiagnosticLevel(diag::warn_double_lock, SourceLocation()) != DiagnosticsEngine::Ignored); - DefaultPolicy.enableConsumedAnalysis = consumed::checkEnabled(D); - + DefaultPolicy.enableConsumedAnalysis = + (unsigned)(D.getDiagnosticLevel(diag::warn_use_while_consumed, + SourceLocation()) != + DiagnosticsEngine::Ignored); } static void flushDiagnostics(Sema &S, sema::FunctionScopeInfo *fscope) { @@ -1727,7 +1729,7 @@ AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, // Check for violations of consumed properties. if (P.enableConsumedAnalysis) { consumed::ConsumedWarningsHandler WarningHandler(S); - consumed::ConsumedAnalyzer Analyzer(S, WarningHandler); + consumed::ConsumedAnalyzer Analyzer(WarningHandler); Analyzer.run(AC); }