Browse Source

Refactor to make MacroState ownership and lifetime clearer.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236032 91177308-0d34-0410-b5e6-96231b3b80d8
Richard Smith 10 years ago
parent
commit
6ec0dff0c4
3 changed files with 15 additions and 6 deletions
  1. 11 2
      include/clang/Lex/Preprocessor.h
  2. 4 1
      lib/Lex/PPLexerChange.cpp
  3. 0 3
      lib/Lex/Preprocessor.cpp

+ 11 - 2
include/clang/Lex/Preprocessor.h

@@ -408,11 +408,20 @@ class Preprocessor : public RefCountedBase<Preprocessor> {
   public:
   public:
     MacroState() : MacroState(nullptr) {}
     MacroState() : MacroState(nullptr) {}
     MacroState(MacroDirective *MD) : State(MD) {}
     MacroState(MacroDirective *MD) : State(MD) {}
-    void destroy() {
+    MacroState(MacroState &&O) LLVM_NOEXCEPT : State(O.State) {
+      O.State = (MacroDirective *)nullptr;
+    }
+    MacroState &operator=(MacroState &&O) LLVM_NOEXCEPT {
+      auto S = O.State;
+      O.State = (MacroDirective *)nullptr;
+      State = S;
+      return *this;
+    }
+    ~MacroState() {
       if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
       if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
         Info->~ModuleMacroInfo();
         Info->~ModuleMacroInfo();
-      State = (MacroDirective*)nullptr;
     }
     }
+
     MacroDirective *getLatest() const {
     MacroDirective *getLatest() const {
       if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
       if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
         return Info->MD;
         return Info->MD;

+ 4 - 1
lib/Lex/PPLexerChange.cpp

@@ -638,6 +638,8 @@ void Preprocessor::LeaveSubmodule() {
     bool ExplicitlyPublic = false;
     bool ExplicitlyPublic = false;
     for (auto *MD = Macro.second.getLatest(); MD != SavedInfo.Latest;
     for (auto *MD = Macro.second.getLatest(); MD != SavedInfo.Latest;
          MD = MD->getPrevious()) {
          MD = MD->getPrevious()) {
+      assert(MD && "broken macro directive chain");
+
       // Skip macros defined in other submodules we #included along the way.
       // Skip macros defined in other submodules we #included along the way.
       Module *Mod = getModuleContainingLocation(MD->getLocation());
       Module *Mod = getModuleContainingLocation(MD->getLocation());
       if (Mod != Info.M)
       if (Mod != Info.M)
@@ -673,7 +675,8 @@ void Preprocessor::LeaveSubmodule() {
     Info.M->NameVisibility = Module::MacrosVisible;
     Info.M->NameVisibility = Module::MacrosVisible;
     Info.M->MacroVisibilityLoc = Info.ImportLoc;
     Info.M->MacroVisibilityLoc = Info.ImportLoc;
     ++MacroVisibilityGeneration;
     ++MacroVisibilityGeneration;
-    // FIXME: Also mark any exported macros as visible, and check for conflicts.
+    // FIXME: Also mark any exported modules as visible, and check for
+    // conflicts.
   }
   }
 
 
   BuildingSubmoduleStack.pop_back();
   BuildingSubmoduleStack.pop_back();

+ 0 - 3
lib/Lex/Preprocessor.cpp

@@ -142,9 +142,6 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts,
 Preprocessor::~Preprocessor() {
 Preprocessor::~Preprocessor() {
   assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
   assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
 
 
-  for (auto &Macro : Macros)
-    Macro.second.destroy();
-
   IncludeMacroStack.clear();
   IncludeMacroStack.clear();
 
 
   // Destroy any macro definitions.
   // Destroy any macro definitions.