<commit_before>//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author:  <NAME> <<EMAIL>>
//
// 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/Interpreter/LookupHelper.h"
#include "cling/Utils/Output.h"

#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"
#include "clang/Parse/Parser.h"
#include "clang/Parse/RAIIObjectsForParser.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Overload.h"
#include "clang/Sema/Sema.h"
#include "clang/Sema/Template.h"
#include "clang/Sema/TemplateDeduction.h"

using namespace clang;

namespace cling {
  ///\brief Class to help with the custom allocation of clang::Expr
  ///
  struct ExprAlloc {
     char fBuffer[sizeof(clang::OpaqueValueExpr)];
  };

  class StartParsingRAII {
    LookupHelper& m_LH;
    llvm::SaveAndRestore<bool> SaveIsRecursivelyRunning;
    // Save and restore the state of the Parser and lexer.
    // Note: ROOT::Internal::ParsingStateRAII also save and restore the state of
    // Sema, including pending instantiation for example.  It is not clear
    // whether we need to do so here too or whether we need to also see the
    // "on-going" semantic information ... For now, we leave Sema untouched.
    clang::Preprocessor::CleanupAndRestoreCacheRAII fCleanupRAII;
    clang::Parser::ParserCurTokRestoreRAII fSavedCurToken;
    ParserStateRAII ResetParserState;
    void prepareForParsing(llvm::StringRef code, llvm::StringRef bufferName,
                           LookupHelper::DiagSetting diagOnOff);
  public:
    StartParsingRAII(LookupHelper& LH, llvm::StringRef code,
                     llvm::StringRef bufferName,
                     LookupHelper::DiagSetting diagOnOff)
        : m_LH(LH), SaveIsRecursivelyRunning(LH.IsRecursivelyRunning),
          fCleanupRAII(LH.m_Parser.get()->getPreprocessor()),
          fSavedCurToken(*LH.m_Parser.get()),
          ResetParserState(*LH.m_Parser.get(),
                           !LH.IsRecursivelyRunning /*skipToEOF*/) {
      LH.IsRecursivelyRunning = true;
      prepareForParsing(code, bufferName, diagOnOff);
    }

    ~StartParsingRAII() { pop(); }
    void pop() const {}
  };

  void StartParsingRAII::prepareForParsing(llvm::StringRef code,
                                           llvm::StringRef bufferName,
                                          LookupHelper::DiagSetting diagOnOff) {
    ++m_LH.m_TotalParseRequests;
    Parser& P = *m_LH.m_Parser.get();
    Sema& S = P.getActions();
    Preprocessor& PP = P.getPreprocessor();
    //
    //  Tell the diagnostic engine to ignore all diagnostics.
    //
    P.getActions().getDiagnostics().setSuppressAllDiagnostics(
                                      diagOnOff == LookupHelper::NoDiagnostics);
    PP.getDiagnostics().setSuppressAllDiagnostics(
                                      diagOnOff == LookupHelper::NoDiagnostics);
    //
    //  Tell Sema we are not in the process of doing an instantiation.
    //
    P.getActions().InNonInstantiationSFINAEContext = true;
    //
    //  Tell the parser to not attempt spelling correction.
    //
    const_cast<LangOptions&>(PP.getLangOpts()).SpellChecking = 0;
    //
    //  Turn on ignoring of the main file eof token.
    //
    //  Note: We need this because token readahead in the following
    //        routine calls ends up parsing it multiple times.
    //
    if (!PP.isIncrementalProcessingEnabled()) {
      PP.enableIncrementalProcessing();
    }
    assert(!code.empty() &&
           "prepareForParsing should only be called when need");

    // Create a fake file to parse the type name.
    FileID FID;<commit_msg>[cling] Make use of shared_ptr operators instead of get() (NFC).<commit_after>//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author:  <NAME> <<EMAIL>>
//
// 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/Interpreter/LookupHelper.h"
#include "cling/Utils/Output.h"

#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"
#include "clang/Parse/Parser.h"
#include "clang/Parse/RAIIObjectsForParser.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Overload.h"
#include "clang/Sema/Sema.h"
#include "clang/Sema/Template.h"
#include "clang/Sema/TemplateDeduction.h"

using namespace clang;

namespace cling {
  ///\brief Class to help with the custom allocation of clang::Expr
  ///
  struct ExprAlloc {
     char fBuffer[sizeof(clang::OpaqueValueExpr)];
  };

  class StartParsingRAII {
    LookupHelper& m_LH;
    llvm::SaveAndRestore<bool> SaveIsRecursivelyRunning;
    // Save and restore the state of the Parser and lexer.
    // Note: ROOT::Internal::ParsingStateRAII also save and restore the state of
    // Sema, including pending instantiation for example.  It is not clear
    // whether we need to do so here too or whether we need to also see the
    // "on-going" semantic information ... For now, we leave Sema untouched.
    clang::Preprocessor::CleanupAndRestoreCacheRAII fCleanupRAII;
    clang::Parser::ParserCurTokRestoreRAII fSavedCurToken;
    ParserStateRAII ResetParserState;
    void prepareForParsing(llvm::StringRef code, llvm::StringRef bufferName,
                           LookupHelper::DiagSetting diagOnOff);
  public:
    StartParsingRAII(LookupHelper& LH, llvm::StringRef code,
                     llvm::StringRef bufferName,
                     LookupHelper::DiagSetting diagOnOff)
        : m_LH(LH), SaveIsRecursivelyRunning(LH.IsRecursivelyRunning),
          fCleanupRAII(LH.m_Parser->getPreprocessor()),
          fSavedCurToken(*LH.m_Parser),
          ResetParserState(*LH.m_Parser,
                           !LH.IsRecursivelyRunning /*skipToEOF*/) {
      LH.IsRecursivelyRunning = true;
      prepareForParsing(code, bufferName, diagOnOff);
    }

    ~StartParsingRAII() { pop(); }
    void pop() const {}
  };

  void StartParsingRAII::prepareForParsing(llvm::StringRef code,
                                           llvm::StringRef bufferName,
                                          LookupHelper::DiagSetting diagOnOff) {
    ++m_LH.m_TotalParseRequests;
    Parser& P = *m_LH.m_Parser;
    Sema& S = P.getActions();
    Preprocessor& PP = P.getPreprocessor();
    //
    //  Tell the diagnostic engine to ignore all diagnostics.
    //
    P.getActions().getDiagnostics().setSuppressAllDiagnostics(
                                      diagOnOff == LookupHelper::NoDiagnostics);
    PP.getDiagnostics().setSuppressAllDiagnostics(
                                      diagOnOff == LookupHelper::NoDiagnostics);
    //
    //  Tell Sema we are not in the process of doing an instantiation.
    //
    P.getActions().InNonInstantiationSFINAEContext = true;
    //
    //  Tell the parser to not attempt spelling correction.
    //
    const_cast<LangOptions&>(PP.getLangOpts()).SpellChecking = 0;
    //
    //  Turn on ignoring of the main file eof token.
    //
    //  Note: We need this because token readahead in the following
    //        routine calls ends up parsing it multiple times.
    //
    if (!PP.isIncrementalProcessingEnabled()) {
      PP.enableIncrementalProcessing();
    }
    assert(!code.empty() &&
           "prepareForParsing should only be called when need");

    // Create a fake file to parse the type name.
    FileID FID;
{'fraction_non_alphanumeric': 0.14139817629179333, 'fraction_numerical': 0.00024316109422492402, 'mean_word_length': 2.414694894146949, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3101627', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2359, 'n_words': 592}