From 2e4cf967b52b1293ce21330be997831212ef68ad Mon Sep 17 00:00:00 2001 From: Axel Naumann Date: Mon, 7 Nov 2016 15:02:42 +0100 Subject: [PATCH] Split ParserStateRAII into Utils/. We need it in other places. --- .../include/cling/Utils/ParserStateRAII.h | 49 ++++++++++++ .../cling/lib/Interpreter/LookupHelper.cpp | 77 +------------------ interpreter/cling/lib/Utils/CMakeLists.txt | 1 + .../cling/lib/Utils/ParserStateRAII.cpp | 63 +++++++++++++++ 4 files changed, 114 insertions(+), 76 deletions(-) create mode 100644 interpreter/cling/include/cling/Utils/ParserStateRAII.h create mode 100644 interpreter/cling/lib/Utils/ParserStateRAII.cpp diff --git a/interpreter/cling/include/cling/Utils/ParserStateRAII.h b/interpreter/cling/include/cling/Utils/ParserStateRAII.h new file mode 100644 index 0000000000000..dfed27693dad3 --- /dev/null +++ b/interpreter/cling/include/cling/Utils/ParserStateRAII.h @@ -0,0 +1,49 @@ +//--------------------------------------------------------------------*- C++ -*- +// CLING - the C++ LLVM-based InterpreterG :) +// author: Vassil Vassilev +// +// This file is dual-licensed: you can choose to license it under the University +// of Illinois Open Source License or the GNU Lesser General Public License. See +// LICENSE.TXT for details. +//------------------------------------------------------------------------------ + +#ifndef CLING_UTILS_ParserStateRAII_H +#define CLING_UTILS_ParserStateRAII_H + +#include "clang/Basic/SourceLocation.h" +#include "clang/Parse/Parser.h" + +namespace clang { + class Preprocessor; +} + +namespace cling { + ///\brief Cleanup Parser state after a failed lookup. + /// + /// After a failed lookup we need to discard the remaining unparsed input, + /// restore the original state of the incremental parsing flag, clear any + /// pending diagnostics, restore the suppress diagnostics flag, and restore + /// the spell checking language options. + /// + class ParserStateRAII { + private: + clang::Parser* P; + clang::Preprocessor& PP; + decltype(clang::Parser::TemplateIds) OldTemplateIds; + bool ResetIncrementalProcessing; + bool OldSuppressAllDiagnostics; + bool OldPPSuppressAllDiagnostics; + bool OldSpellChecking; + clang::SourceLocation OldPrevTokLocation; + unsigned short OldParenCount, OldBracketCount, OldBraceCount; + unsigned OldTemplateParameterDepth; + bool OldInNonInstantiationSFINAEContext; + + public: + ParserStateRAII(clang::Parser& p); + ~ParserStateRAII(); + +}; + +} // end namespace cling +#endif // CLING_UTILS_ParserStateRAII_H diff --git a/interpreter/cling/lib/Interpreter/LookupHelper.cpp b/interpreter/cling/lib/Interpreter/LookupHelper.cpp index 9baddcefff458..b5565753db73e 100644 --- a/interpreter/cling/lib/Interpreter/LookupHelper.cpp +++ b/interpreter/cling/lib/Interpreter/LookupHelper.cpp @@ -12,6 +12,7 @@ #include "DeclUnloader.h" #include "cling/Interpreter/Interpreter.h" #include "cling/Utils/AST.h" +#include "cling/Utils/ParserStateRAII.h" #include "clang/AST/ASTContext.h" #include "clang/Frontend/CompilerInstance.h" @@ -26,82 +27,6 @@ using namespace clang; -namespace cling { - - ///\brief Cleanup Parser state after a failed lookup. - /// - /// After a failed lookup we need to discard the remaining unparsed input, - /// restore the original state of the incremental parsing flag, clear any - /// pending diagnostics, restore the suppress diagnostics flag, and restore - /// the spell checking language options. - /// - class ParserStateRAII { - private: - Parser* P; - Preprocessor& PP; - decltype(Parser::TemplateIds) OldTemplateIds; - bool ResetIncrementalProcessing; - bool OldSuppressAllDiagnostics; - bool OldPPSuppressAllDiagnostics; - bool OldSpellChecking; - SourceLocation OldPrevTokLocation; - unsigned short OldParenCount, OldBracketCount, OldBraceCount; - unsigned OldTemplateParameterDepth; - bool OldInNonInstantiationSFINAEContext; - - public: - ParserStateRAII(Parser& p) - : P(&p), PP(p.getPreprocessor()), - ResetIncrementalProcessing(p.getPreprocessor() - .isIncrementalProcessingEnabled()), - OldSuppressAllDiagnostics(P->getActions().getDiagnostics() - .getSuppressAllDiagnostics()), - OldPPSuppressAllDiagnostics(p.getPreprocessor().getDiagnostics() - .getSuppressAllDiagnostics()), - OldSpellChecking(p.getPreprocessor().getLangOpts().SpellChecking), - OldPrevTokLocation(p.PrevTokLocation), - OldParenCount(p.ParenCount), OldBracketCount(p.BracketCount), - OldBraceCount(p.BraceCount), - OldTemplateParameterDepth(p.TemplateParameterDepth), - OldInNonInstantiationSFINAEContext(P->getActions() - .InNonInstantiationSFINAEContext) - { - OldTemplateIds.swap(P->TemplateIds); - } - - ~ParserStateRAII() - { - // - // Advance the parser to the end of the file, and pop the include stack. - // - // Note: Consuming the EOF token will pop the include stack. - // - { - // Cleanup the TemplateIds before swapping the previous set back. - DestroyTemplateIdAnnotationsRAIIObj CleanupTemplateIds(*P); - } - P->TemplateIds.swap(OldTemplateIds); - P->SkipUntil(tok::eof); - PP.enableIncrementalProcessing(ResetIncrementalProcessing); - // Doesn't reset the diagnostic mappings - P->getActions().getDiagnostics().Reset(/*soft=*/true); - P->getActions().getDiagnostics().setSuppressAllDiagnostics(OldSuppressAllDiagnostics); - PP.getDiagnostics().Reset(/*soft=*/true); - PP.getDiagnostics().setSuppressAllDiagnostics(OldPPSuppressAllDiagnostics); - const_cast(PP.getLangOpts()).SpellChecking = - OldSpellChecking; - - P->PrevTokLocation = OldPrevTokLocation; - P->ParenCount = OldParenCount; - P->BracketCount = OldBracketCount; - P->BraceCount = OldBraceCount; - P->TemplateParameterDepth = OldTemplateParameterDepth; - P->getActions().InNonInstantiationSFINAEContext = - OldInNonInstantiationSFINAEContext; - } - }; -} - namespace cling { ///\brief Class to help with the custom allocation of clang::Expr /// diff --git a/interpreter/cling/lib/Utils/CMakeLists.txt b/interpreter/cling/lib/Utils/CMakeLists.txt index beb78c0743201..bf267c56a150e 100644 --- a/interpreter/cling/lib/Utils/CMakeLists.txt +++ b/interpreter/cling/lib/Utils/CMakeLists.txt @@ -11,6 +11,7 @@ set( LLVM_LINK_COMPONENTS add_cling_library(clingUtils OBJECT AST.cpp + ParserStateRAII.cpp Paths.cpp PlatformMac.cpp PlatformPosix.cpp diff --git a/interpreter/cling/lib/Utils/ParserStateRAII.cpp b/interpreter/cling/lib/Utils/ParserStateRAII.cpp new file mode 100644 index 0000000000000..60a69024cd253 --- /dev/null +++ b/interpreter/cling/lib/Utils/ParserStateRAII.cpp @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// CLING - the C++ LLVM-based InterpreterG :) +// author: Vassil Vassilev +// +// This file is dual-licensed: you can choose to license it under the University +// of Illinois Open Source License or the GNU Lesser General Public License. See +// LICENSE.TXT for details. +//------------------------------------------------------------------------------ + +#include "cling/Utils/ParserStateRAII.h" + +#include "clang/Parse/RAIIObjectsForParser.h" + +using namespace clang; + +cling::ParserStateRAII::ParserStateRAII(Parser& p) + : P(&p), PP(p.getPreprocessor()), + ResetIncrementalProcessing(p.getPreprocessor() + .isIncrementalProcessingEnabled()), + OldSuppressAllDiagnostics(P->getActions().getDiagnostics() + .getSuppressAllDiagnostics()), + OldPPSuppressAllDiagnostics(p.getPreprocessor().getDiagnostics() + .getSuppressAllDiagnostics()), + OldSpellChecking(p.getPreprocessor().getLangOpts().SpellChecking), + OldPrevTokLocation(p.PrevTokLocation), + OldParenCount(p.ParenCount), OldBracketCount(p.BracketCount), + OldBraceCount(p.BraceCount), + OldTemplateParameterDepth(p.TemplateParameterDepth), + OldInNonInstantiationSFINAEContext(P->getActions() + .InNonInstantiationSFINAEContext) +{ + OldTemplateIds.swap(P->TemplateIds); +} + +cling::ParserStateRAII::~ParserStateRAII() { + // + // Advance the parser to the end of the file, and pop the include stack. + // + // Note: Consuming the EOF token will pop the include stack. + // + { + // Cleanup the TemplateIds before swapping the previous set back. + DestroyTemplateIdAnnotationsRAIIObj CleanupTemplateIds(*P); + } + P->TemplateIds.swap(OldTemplateIds); + P->SkipUntil(tok::eof); + PP.enableIncrementalProcessing(ResetIncrementalProcessing); + // Doesn't reset the diagnostic mappings + P->getActions().getDiagnostics().Reset(/*soft=*/true); + P->getActions().getDiagnostics().setSuppressAllDiagnostics(OldSuppressAllDiagnostics); + PP.getDiagnostics().Reset(/*soft=*/true); + PP.getDiagnostics().setSuppressAllDiagnostics(OldPPSuppressAllDiagnostics); + const_cast(PP.getLangOpts()).SpellChecking = + OldSpellChecking; + + P->PrevTokLocation = OldPrevTokLocation; + P->ParenCount = OldParenCount; + P->BracketCount = OldBracketCount; + P->BraceCount = OldBraceCount; + P->TemplateParameterDepth = OldTemplateParameterDepth; + P->getActions().InNonInstantiationSFINAEContext = + OldInNonInstantiationSFINAEContext; +}