]> granicus.if.org Git - clang/commitdiff
Refactoring.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 19 Nov 2010 00:19:12 +0000 (00:19 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 19 Nov 2010 00:19:12 +0000 (00:19 +0000)
Move ErrorTrap from clang/Sema to clang/Basic as DiagnosticErrorTrap and use it in Scope.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@119763 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/Diagnostic.h
include/clang/Sema/Scope.h
include/clang/Sema/Sema.h
lib/Parse/Parser.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaDeclCXX.cpp

index 1f3b2cd5f47ca86c32a5ac87baef021bde2810ef..b38df5f9c4f7db84ac85b5538b80c9c1d184262d 100644 (file)
@@ -29,6 +29,7 @@ namespace clang {
   class DeclContext;
   class LangOptions;
   class Preprocessor;
+  class DiagnosticErrorTrap;
 
 /// \brief Annotates a diagnostic with some code that should be
 /// inserted, removed, or replaced to fix the problem.
@@ -486,6 +487,7 @@ private:
   friend class DiagnosticBuilder;
   friend class DiagnosticInfo;
   friend class PartialDiagnostic;
+  friend class DiagnosticErrorTrap;
   
   /// CurDiagLoc - This is the location of the current diagnostic that is in
   /// flight.
@@ -549,6 +551,27 @@ private:
   friend class ASTWriter;
 };
 
+/// \brief RAII class that determines when any errors have occurred
+/// between the time the instance was created and the time it was
+/// queried.
+class DiagnosticErrorTrap {
+  Diagnostic &Diag;
+  unsigned PrevErrors;
+
+public:
+  explicit DiagnosticErrorTrap(Diagnostic &Diag)
+    : Diag(Diag), PrevErrors(Diag.NumErrors) {}
+
+  /// \brief Determine whether any errors have occurred since this
+  /// object instance was created.
+  bool hasErrorOccurred() const {
+    return Diag.NumErrors > PrevErrors;
+  }
+
+  // Set to initial state of "no errors occurred".
+  void reset() { PrevErrors = Diag.NumErrors; }
+};
+
 //===----------------------------------------------------------------------===//
 // DiagnosticBuilder
 //===----------------------------------------------------------------------===//
index 4229c6c627488ff3c497d9b425c3f7f63a8a99d7..ad3ad4df136fbd290216f88e3b7620ef8567c9b5 100644 (file)
@@ -14,6 +14,7 @@
 #ifndef LLVM_CLANG_SEMA_SCOPE_H
 #define LLVM_CLANG_SEMA_SCOPE_H
 
+#include "clang/Basic/Diagnostic.h"
 #include "llvm/ADT/SmallPtrSet.h"
 
 namespace clang {
@@ -131,11 +132,12 @@ private:
   typedef llvm::SmallVector<UsingDirectiveDecl *, 2> UsingDirectivesTy;
   UsingDirectivesTy UsingDirectives;
 
-  /// \brief The number of errors at the start of the given scope.
-  unsigned NumErrorsAtStart;
+  /// \brief Used to determine if errors occurred in this scope.
+  DiagnosticErrorTrap ErrorTrap;
   
 public:
-  Scope(Scope *Parent, unsigned ScopeFlags) {
+  Scope(Scope *Parent, unsigned ScopeFlags, Diagnostic &Diag)
+    : ErrorTrap(Diag) {
     Init(Parent, ScopeFlags);
   }
 
@@ -214,13 +216,7 @@ public:
   void* getEntity() const { return Entity; }
   void setEntity(void *E) { Entity = E; }
 
-  /// \brief Retrieve the number of errors that had been emitted when we
-  /// entered this scope.
-  unsigned getNumErrorsAtStart() const { return NumErrorsAtStart; }
-  
-  void setNumErrorsAtStart(unsigned NumErrors) {
-    NumErrorsAtStart = NumErrors;
-  }
+  bool hasErrorOccurred() const { return ErrorTrap.hasErrorOccurred(); }
                            
   /// isClassScope - Return true if this scope is a class/struct/union scope.
   bool isClassScope() const {
@@ -318,7 +314,7 @@ public:
     DeclsInScope.clear();
     UsingDirectives.clear();
     Entity = 0;
-    NumErrorsAtStart = 0;
+    ErrorTrap.reset();
   }
 };
 
index 1790351f63267e7bf6d79af4fd67cc93cf91b49a..e6cbc329bf5e58ade32a3ceb95cbbcf634f74d1d 100644 (file)
@@ -3521,24 +3521,6 @@ public:
     }
   };
 
-  /// \brief RAII class that determines when any errors have occurred
-  /// between the time the instance was created and the time it was
-  /// queried.
-  class ErrorTrap {
-    Sema &SemaRef;
-    unsigned PrevErrors;
-
-  public:
-    explicit ErrorTrap(Sema &SemaRef)
-      : SemaRef(SemaRef), PrevErrors(SemaRef.getDiagnostics().getNumErrors()) {}
-
-    /// \brief Determine whether any errors have occurred since this
-    /// object instance was created.
-    bool hasErrorOccurred() const {
-      return SemaRef.getDiagnostics().getNumErrors() > PrevErrors;
-    }
-  };
-
   /// \brief The current instantiation scope used to store local
   /// variables.
   LocalInstantiationScope *CurrentInstantiationScope;
index 918c6718fcb345a8e65c6b539de250f7c2bc99b6..fefe7871df00bb66c92a6ac9bd2d45a871e0501a 100644 (file)
@@ -305,9 +305,8 @@ void Parser::EnterScope(unsigned ScopeFlags) {
     N->Init(getCurScope(), ScopeFlags);
     Actions.CurScope = N;
   } else {
-    Actions.CurScope = new Scope(getCurScope(), ScopeFlags);
+    Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
   }
-  getCurScope()->setNumErrorsAtStart(Diags.getNumErrors());
 }
 
 /// ExitScope - Pop a scope off the scope stack.
index ceec0016e9ad3795b9730aa03d6b9b60ec98b5c5..7c2a8fb105ef3cfcfaefab358936417d11d674ec 100644 (file)
@@ -696,7 +696,7 @@ void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
     if (!D->getDeclName()) continue;
 
     // Diagnose unused variables in this scope.
-    if (S->getNumErrorsAtStart() == getDiagnostics().getNumErrors())
+    if (!S->hasErrorOccurred())
       DiagnoseUnusedDecl(D);
     
     // Remove this name from our lexical scope.
index 7217b25652bd7bb81e9e089c12d09066fced8ffb..38c1c3928d9156a5e9291984a18095bf352a1482 100644 (file)
@@ -4365,7 +4365,7 @@ void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation,
   assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor");
 
   ImplicitlyDefinedFunctionScope Scope(*this, Constructor);
-  ErrorTrap Trap(*this);
+  DiagnosticErrorTrap Trap(Diags);
   if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false) ||
       Trap.hasErrorOccurred()) {
     Diag(CurrentLocation, diag::note_member_synthesized_at) 
@@ -4473,7 +4473,7 @@ void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation,
 
   ImplicitlyDefinedFunctionScope Scope(*this, Destructor);
 
-  ErrorTrap Trap(*this);
+  DiagnosticErrorTrap Trap(Diags);
   MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(),
                                          Destructor->getParent());
 
@@ -4878,7 +4878,7 @@ void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation,
   CopyAssignOperator->setUsed();
 
   ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator);
-  ErrorTrap Trap(*this);
+  DiagnosticErrorTrap Trap(Diags);
 
   // C++0x [class.copy]p30:
   //   The implicitly-defined or explicitly-defaulted copy assignment operator
@@ -5340,7 +5340,7 @@ void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation,
   assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor");
 
   ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor);
-  ErrorTrap Trap(*this);
+  DiagnosticErrorTrap Trap(Diags);
 
   if (SetBaseOrMemberInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) ||
       Trap.hasErrorOccurred()) {