Skip to content

Commit

Permalink
Split ParserStateRAII into Utils/. We need it in other places.
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel-Naumann committed Nov 7, 2016
1 parent 6f16264 commit 2e4cf96
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 76 deletions.
49 changes: 49 additions & 0 deletions interpreter/cling/include/cling/Utils/ParserStateRAII.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//--------------------------------------------------------------------*- C++ -*-
// CLING - the C++ LLVM-based InterpreterG :)
// author: Vassil Vassilev <[email protected]>
//
// 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
77 changes: 1 addition & 76 deletions interpreter/cling/lib/Interpreter/LookupHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<LangOptions&>(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
///
Expand Down
1 change: 1 addition & 0 deletions interpreter/cling/lib/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set( LLVM_LINK_COMPONENTS

add_cling_library(clingUtils OBJECT
AST.cpp
ParserStateRAII.cpp
Paths.cpp
PlatformMac.cpp
PlatformPosix.cpp
Expand Down
63 changes: 63 additions & 0 deletions interpreter/cling/lib/Utils/ParserStateRAII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author: Vassil Vassilev <[email protected]>
//
// 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<LangOptions&>(PP.getLangOpts()).SpellChecking =
OldSpellChecking;

P->PrevTokLocation = OldPrevTokLocation;
P->ParenCount = OldParenCount;
P->BracketCount = OldBracketCount;
P->BraceCount = OldBraceCount;
P->TemplateParameterDepth = OldTemplateParameterDepth;
P->getActions().InNonInstantiationSFINAEContext =
OldInNonInstantiationSFINAEContext;
}

0 comments on commit 2e4cf96

Please sign in to comment.