Add Interpreter::getMacroValue method.

This commit is contained in:
Frederich Munch 2017-02-22 15:31:26 -05:00 committed by sftnight
parent 9b3084f955
commit f28c04c1a2
4 changed files with 62 additions and 14 deletions

View File

@ -723,6 +723,16 @@ namespace cling {
///
const clang::MacroInfo* getMacro(llvm::StringRef Name) const;
///\brief Get a given macro value by name.
///
///\param[in] Name - the name of the macro to look for
///\param[out] Strip - characters to remove from the value.
///
///\returns the macro's value if the macro was defined, otherwise empty
///
std::string getMacroValue(llvm::StringRef Name,
const char* Strip = "\"") const;
///\brief Add an atexit function.
///
///\param[in] Func - Function to be called.

View File

@ -73,20 +73,9 @@ namespace {
#error "Unknown platform for ABI check";
#endif
llvm::StringRef CurABI;
if (const clang::MacroInfo* MI = Interp.getMacro(CLING_CXXABI_NAME)) {
const clang::Token* Tok = MI->getNumTokens() == 1 ?
MI->tokens_begin() : nullptr;
if (Tok && Tok->isLiteral()) {
// Tok::getLiteralData can fail even if Tok::isLiteral is true!
SmallString<64> Buffer;
CurABI = Interp.getCI()->getPreprocessor().getSpelling(*Tok, Buffer);
// Strip any quotation marks.
CurABI = CurABI.trim("\"");
if (CurABI.equals(CLING_CXXABI_VERS))
return true;
}
}
const std::string CurABI = Interp.getMacroValue(CLING_CXXABI_NAME);
if (CurABI == CLING_CXXABI_VERS)
return true;
cling::errs() <<
"Warning in cling::IncrementalParser::CheckABICompatibility():\n"

View File

@ -560,6 +560,21 @@ namespace cling {
return nullptr;
}
std::string Interpreter::getMacroValue(llvm::StringRef Macro,
const char* Trim) const {
std::string Value;
if (const MacroInfo* MI = getMacro(Macro)) {
for (const clang::Token& Tok : MI->tokens()) {
llvm::SmallString<64> Buffer;
Macro = getCI()->getPreprocessor().getSpelling(Tok, Buffer);
if (!Value.empty())
Value += " ";
Value += Trim ? Macro.trim(Trim).str() : Macro.str();
}
}
return Value;
}
///\brief Maybe transform the input line to implement cint command line
/// semantics (declarations are global) and compile to produce a module.
///

34
test/Interfaces/Macro.C Normal file
View File

@ -0,0 +1,34 @@
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
//
// 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.
//------------------------------------------------------------------------------
// RUN: cat %s | %cling -Xclang -verify 2>&1 | FileCheck %s
#include "cling/Interpreter/Interpreter.h"
#define TEST01 "A B C D E F"
gCling->getMacroValue("TEST01")
// CHECK: (std::string) "A B C D E F"
#define TEST02 0 1 2 3 4 5 6 7
gCling->getMacroValue("TEST02")
// CHECK-NEXT: (std::string) "0 1 2 3 4 5 6 7"
#define TEST03 STRIP "STRING" TEST
gCling->getMacroValue("TEST03")
// CHECK-NEXT: (std::string) "STRIP STRING TEST"
#define TEST03 STRIP "STRING" TEST
gCling->getMacroValue("TEST03", 0)
// CHECK-NEXT: (std::string) "STRIP "STRING" TEST"
#define TEST04(A,B,C) A ##B #C
gCling->getMacroValue("TEST04")
// CHECK-NEXT: (std::string) "A ## B # C"
// expected-no-diagnostics
.q