r215780 - Add a RAII class for saving and restoring instantiations and uses. No behavior change.

Richard Smith richard at metafoo.co.uk
Wed Sep 24 11:58:08 PDT 2014


On Fri, Aug 15, 2014 at 3:29 PM, Nico Weber <nicolasweber at gmx.de> wrote:

> Author: nico
> Date: Fri Aug 15 17:29:14 2014
> New Revision: 215780
>
> URL: http://llvm.org/viewvc/llvm-project?rev=215780&view=rev
> Log:
> Add a RAII class for saving and restoring instantiations and uses. No
> behavior change.
>
> Modified:
>     cfe/trunk/include/clang/Sema/Sema.h
>     cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
>
> Modified: cfe/trunk/include/clang/Sema/Sema.h
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=215780&r1=215779&r2=215780&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Sema/Sema.h (original)
> +++ cfe/trunk/include/clang/Sema/Sema.h Fri Aug 15 17:29:14 2014
> @@ -6568,6 +6568,31 @@ public:
>    /// but have not yet been performed.
>    std::deque<PendingImplicitInstantiation> PendingInstantiations;
>
> +  class SavePendingInstantiationsAndVTableUsesRAII {
> +  public:
> +    SavePendingInstantiationsAndVTableUsesRAII(Sema &S): S(S) {
> +      SavedPendingInstantiations.swap(S.PendingInstantiations);
> +      SavedVTableUses.swap(S.VTableUses);
> +    }
> +
> +    ~SavePendingInstantiationsAndVTableUsesRAII() {
> +      // Restore the set of pending vtables.
> +      assert(S.VTableUses.empty() &&
> +             "VTableUses should be empty before it is discarded.");
> +      S.VTableUses.swap(SavedVTableUses);
> +
> +      // Restore the set of pending implicit instantiations.
> +      assert(S.PendingInstantiations.empty() &&
> +             "PendingInstantiations should be empty before it is
> discarded.");
> +      S.PendingInstantiations.swap(SavedPendingInstantiations);
> +    }
> +
> +  private:
> +    Sema &S;
> +    SmallVector<VTableUse, 16> SavedVTableUses;
> +    std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
> +  };
> +
>    /// \brief The queue of implicit template instantiations that are
> required
>    /// and must be performed within the current local scope.
>    ///
>
> Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=215780&r1=215779&r2=215780&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Fri Aug 15 17:29:14
> 2014
> @@ -3389,13 +3389,13 @@ void Sema::InstantiateFunctionDefinition
>    // If we're performing recursive template instantiation, create our own
>    // queue of pending implicit instantiations that we will instantiate
> later,
>    // while we're still within our own instantiation context.
> -  SmallVector<VTableUse, 16> SavedVTableUses;
> -  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
>    SavePendingLocalImplicitInstantiationsRAII
>        SavedPendingLocalImplicitInstantiations(*this);
> +  std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII>
> +      SavePendingInstantiationsAndVTableUses;
>    if (Recursive) {
> -    VTableUses.swap(SavedVTableUses);
> -    PendingInstantiations.swap(SavedPendingInstantiations);
> +    SavePendingInstantiationsAndVTableUses.reset(
> +        new SavePendingInstantiationsAndVTableUsesRAII(*this));
>

It seems wasteful to go to the heap for this each time; maybe instead pass
a flag to the RAII object to tell it whether to do this save/load instead?


>    }
>
>    EnterExpressionEvaluationContext EvalContext(*this,
> @@ -3466,15 +3466,8 @@ void Sema::InstantiateFunctionDefinition
>      // instantiation of this template.
>      PerformPendingInstantiations();
>
> -    // Restore the set of pending vtables.
> -    assert(VTableUses.empty() &&
> -           "VTableUses should be empty before it is discarded.");
> -    VTableUses.swap(SavedVTableUses);
> -
> -    // Restore the set of pending implicit instantiations.
> -    assert(PendingInstantiations.empty() &&
> -           "PendingInstantiations should be empty before it is
> discarded.");
> -    PendingInstantiations.swap(SavedPendingInstantiations);
> +    // Restore PendingInstantiations and VTableUses.
> +    SavePendingInstantiationsAndVTableUses.reset();
>    }
>  }
>
> @@ -3790,11 +3783,11 @@ void Sema::InstantiateVariableDefinition
>        // If we're performing recursive template instantiation, create our
> own
>        // queue of pending implicit instantiations that we will instantiate
>        // later, while we're still within our own instantiation context.
> -      SmallVector<VTableUse, 16> SavedVTableUses;
> -      std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
> +      std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII>
> +          SavePendingInstantiationsAndVTableUses;
>        if (Recursive) {
> -        VTableUses.swap(SavedVTableUses);
> -        PendingInstantiations.swap(SavedPendingInstantiations);
> +        SavePendingInstantiationsAndVTableUses.reset(
> +            new SavePendingInstantiationsAndVTableUsesRAII(*this));
>        }
>
>        LocalInstantiationScope Local(*this);
> @@ -3822,15 +3815,8 @@ void Sema::InstantiateVariableDefinition
>          // instantiation of this template.
>          PerformPendingInstantiations();
>
> -        // Restore the set of pending vtables.
> -        assert(VTableUses.empty() &&
> -               "VTableUses should be empty before it is discarded.");
> -        VTableUses.swap(SavedVTableUses);
> -
> -        // Restore the set of pending implicit instantiations.
> -        assert(PendingInstantiations.empty() &&
> -               "PendingInstantiations should be empty before it is
> discarded.");
> -        PendingInstantiations.swap(SavedPendingInstantiations);
> +        // Restore PendingInstantiations and VTableUses.
> +        SavePendingInstantiationsAndVTableUses.reset();
>        }
>      }
>
> @@ -3914,13 +3900,13 @@ void Sema::InstantiateVariableDefinition
>    // If we're performing recursive template instantiation, create our own
>    // queue of pending implicit instantiations that we will instantiate
> later,
>    // while we're still within our own instantiation context.
> -  SmallVector<VTableUse, 16> SavedVTableUses;
> -  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
>    SavePendingLocalImplicitInstantiationsRAII
>        SavedPendingLocalImplicitInstantiations(*this);
> +  std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII>
> +      SavePendingInstantiationsAndVTableUses;
>    if (Recursive) {
> -    VTableUses.swap(SavedVTableUses);
> -    PendingInstantiations.swap(SavedPendingInstantiations);
> +    SavePendingInstantiationsAndVTableUses.reset(
> +        new SavePendingInstantiationsAndVTableUsesRAII(*this));
>    }
>
>    // Enter the scope of this instantiation. We don't use
> @@ -3987,15 +3973,8 @@ void Sema::InstantiateVariableDefinition
>      // instantiation of this template.
>      PerformPendingInstantiations();
>
> -    // Restore the set of pending vtables.
> -    assert(VTableUses.empty() &&
> -           "VTableUses should be empty before it is discarded.");
> -    VTableUses.swap(SavedVTableUses);
> -
> -    // Restore the set of pending implicit instantiations.
> -    assert(PendingInstantiations.empty() &&
> -           "PendingInstantiations should be empty before it is
> discarded.");
> -    PendingInstantiations.swap(SavedPendingInstantiations);
> +    // Restore PendingInstantiations and VTableUses.
> +    SavePendingInstantiationsAndVTableUses.reset();
>    }
>  }
>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140924/d550ed2c/attachment.html>


More information about the cfe-commits mailing list