static Optional<std::pair<llvm::APSInt, Type>> getIntegerConstantForMacroToken(ClangImporter::Implementation &impl, DeclContext *DC, const clang::Token &token) { // Integer literal. if (token.is(clang::tok::numeric_constant)) { if (auto literal = parseNumericLiteral<clang::IntegerLiteral>(impl,token)) { auto value = llvm::APSInt { literal->getValue(), literal->getType()->isUnsignedIntegerType() }; auto type = impl.importType(literal->getType(), ImportTypeKind::Value, isInSystemModule(DC), Bridgeability::None); return {{ value, type }}; } // Macro identifier. } else if (token.is(clang::tok::identifier) && token.getIdentifierInfo()->hasMacroDefinition()) { auto rawID = token.getIdentifierInfo(); auto definition = impl.getClangPreprocessor().getMacroDefinition(rawID); if (!definition) return None; ClangNode macroNode; const clang::MacroInfo *macroInfo; if (definition.getModuleMacros().empty()) { macroInfo = definition.getMacroInfo(); macroNode = macroInfo; } else { // Follow MacroDefinition::getMacroInfo in preferring the last ModuleMacro // rather than the first. const clang::ModuleMacro *moduleMacro = definition.getModuleMacros().back(); macroInfo = moduleMacro->getMacroInfo(); macroNode = moduleMacro; } auto importedID = impl.getNameImporter().importMacroName(rawID, macroInfo); (void)impl.importMacro(importedID, macroNode); auto searcher = impl.ImportedMacroConstants.find(macroInfo); if (searcher == impl.ImportedMacroConstants.end()) { return None; } auto importedConstant = searcher->second; if (!importedConstant.first.isInt()) { return None; } return {{ importedConstant.first.getInt(), importedConstant.second }}; } return None; }
bool PragmaRecorder::GetPragmaValueFromToken(const clang::Token &Token, std::string &PragmaValue) { // Same as the GetPragmaName() if (Token.is(clang::tok::r_paren)) PragmaValue.clear(); else return GetPragmaNameFromToken(Token, PragmaValue); return true; }
bool PragmaRecorder::GetPragmaNameFromToken(const clang::Token &Token, std::string &PragmaName) { if (Token.isLiteral()) PragmaName.assign(Token.getLiteralData(), Token.getLength()); else if (Token.is(clang::tok::identifier)) PragmaName.assign(Token.getIdentifierInfo()->getNameStart(), Token.getIdentifierInfo()->getLength()); else return false; return true; }
static Optional<std::pair<llvm::APSInt, Type>> getIntegerConstantForMacroToken(ClangImporter::Implementation &impl, DeclContext *DC, const clang::Token &token) { // Integer literal. if (token.is(clang::tok::numeric_constant)) { if (auto literal = parseNumericLiteral<clang::IntegerLiteral>(impl,token)) { auto value = llvm::APSInt { literal->getValue(), literal->getType()->isUnsignedIntegerType() }; auto type = impl.importType(literal->getType(), ImportTypeKind::Value, isInSystemModule(DC), /*isFullyBridgeable*/false); return {{ value, type }}; } // Macro identifier. } else if (token.is(clang::tok::identifier) && token.getIdentifierInfo()->hasMacroDefinition()) { auto rawID = token.getIdentifierInfo(); auto macroInfo = impl.getClangPreprocessor().getMacroInfo(rawID); auto importedID = impl.getNameImporter().importMacroName(rawID, macroInfo); impl.importMacro(importedID, macroInfo); auto searcher = impl.ImportedMacroConstants.find(macroInfo); if (searcher == impl.ImportedMacroConstants.end()) { return None; } auto importedConstant = searcher->second; if (!importedConstant.first.isInt()) { return None; } return {{ importedConstant.first.getInt(), importedConstant.second }}; } return None; }
static bool isSignToken(const clang::Token &tok) { return tok.is(clang::tok::plus) || tok.is(clang::tok::minus) || tok.is(clang::tok::tilde); }
static bool isStringToken(const clang::Token &tok) { return tok.is(clang::tok::string_literal) || tok.is(clang::tok::utf8_string_literal); }
static bool isBinaryOperator(const clang::Token &tok) { return tok.is(clang::tok::amp) || tok.is(clang::tok::pipe) || tok.is(clang::tok::ampamp) || tok.is(clang::tok::pipepipe); }