std::string location(clang::SourceLocation const& l, clang::SourceManager const& sm) { if(l.isValid()) { if(l.isFileID()) { // if (sm.isLoadedFileID (sm.getFileID(l))) return "PRELOADED MODULE"; if(sm.isLoadedSourceLocation(l)) { return "PRELOADED MODULE"; } return l.printToString(sm); } if(l.isMacroID()) { // FIXME: what do we do here? somehow clang fails /* std::cout << "SLoc isMacroID\n"; auto sl = sm.getSpellingLoc(l); if (sm.isLoadedSourceLocation(sl) ) { return "PRELOADED MODULE"; } if(sl.isValid()) { return sl.printToString(sm); } PresumedLoc pl = sm.getPresumedLoc(l); if (pl.isValid()){ return string(pl.getFilename()); } */ } return string("UNKNOWN FILE"); } return string("INVALID LOC"); }
ReportStream Reporter::report(const clang::SourceRange &range) { // Track this report mTotalReports++; // Print the expansion location const clang::SourceLocation expansionLoc(mSourceManager.getExpansionLoc(range.getBegin())); std::cerr << boldCode() << expansionLoc.printToString(mSourceManager) << ": "; // Print the "connect" in a different color than warnings or errors std::cerr << connectColorCode() << "connect:" << defaultColorCode(); return ReportStream(); }
void c2ffi::process_macros(clang::CompilerInstance &ci, std::ostream &os) { using namespace c2ffi; clang::SourceManager &sm = ci.getSourceManager(); clang::Preprocessor &pp = ci.getPreprocessor(); for(clang::Preprocessor::macro_iterator i = pp.macro_begin(); i != pp.macro_end(); i++) { const clang::MacroInfo *mi = (*i).second->getMacroInfo(); const clang::SourceLocation sl = mi->getDefinitionLoc(); std::string loc = sl.printToString(sm); const char *name = (*i).first->getNameStart(); if(mi->isBuiltinMacro() || loc.substr(0,10) == "<built-in>") { } else if(mi->isFunctionLike()) { } else if(best_guess type = macro_type(pp, name, mi)) { os << "/* " << loc << " */" << std::endl; os << "#define " << name << " " << macro_to_string(pp, mi) << std::endl << std::endl; } } for(clang::Preprocessor::macro_iterator i = pp.macro_begin(); i != pp.macro_end(); i++) { clang::MacroInfo *mi = (*i).second->getMacroInfo(); clang::SourceLocation sl = mi->getDefinitionLoc(); std::string loc = sl.printToString(sm); const char *name = (*i).first->getNameStart(); if(mi->isBuiltinMacro() || loc.substr(0,10) == "<built-in>") { } else if(mi->isFunctionLike()) { } else if(best_guess type = macro_type(pp, name, mi)) { output_redef(pp, name, mi, type, os); } } }
std::string NamedDeclMatcher::loc(clang::SourceLocation L) { return L.printToString(ci->getSourceManager()); }