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"); }
// Get a "file:line:column" source location string. static std::string getSourceLocationString(clang::Preprocessor &PP, clang::SourceLocation Loc) { if (Loc.isInvalid()) return std::string("(none)"); if (Loc.isFileID()) { clang::PresumedLoc PLoc = PP.getSourceManager().getPresumedLoc(Loc); if (PLoc.isInvalid()) { return std::string("(invalid)"); } std::string Str; llvm::raw_string_ostream SS(Str); // The macro expansion and spelling pos is identical for file locs. SS << "\"" << PLoc.getFilename() << ':' << PLoc.getLine() << ':' << PLoc.getColumn() << "\""; std::string Result = SS.str(); // YAML treats backslash as escape, so use forward slashes. std::replace(Result.begin(), Result.end(), '\\', '/'); return Result; } return std::string("(nonfile)"); }
void PreprocessorCallback::HandlePPCond(clang::SourceLocation Loc, clang::SourceLocation IfLoc) { if (!Loc.isValid() || !Loc.isFileID()) return; clang::SourceManager &SM = annotator.getSourceMgr(); clang::FileID FID = SM.getFileID(Loc); if (!annotator.shouldProcess(FID)) return; while(ElifMapping.count(IfLoc)) { IfLoc = Loc; } if (SM.getFileID(IfLoc) != FID) { return; } annotator.generator(FID).addTag("span", ("data-ppcond=\"" + clang::Twine(SM.getExpansionLineNumber(IfLoc)) + "\"").str(), SM.getFileOffset(Loc), clang::Lexer::MeasureTokenLength(Loc, SM, PP.getLangOpts())); }
void PreprocessorCallback::InclusionDirective(clang::SourceLocation HashLoc, const clang::Token& IncludeTok, llvm::StringRef FileName, bool IsAngled, clang::CharSourceRange FilenameRange, const clang::FileEntry* File, llvm::StringRef SearchPath, llvm::StringRef RelativePath, const clang::Module* Imported) { if (!HashLoc.isValid() || !HashLoc.isFileID() || !File) return; clang::SourceManager &sm = annotator.getSourceMgr(); clang::FileID FID = sm.getFileID(HashLoc); if (!annotator.shouldProcess(FID)) return; std::string link = annotator.pathTo(FID, File); if (link.empty()) return; auto B = sm.getFileOffset(FilenameRange.getBegin()); auto E = sm.getFileOffset(FilenameRange.getEnd()); annotator.generator(FID).addTag("a", "href=\"" % link % "\"", B, E-B); }
void RefFinder::addFileLocation(clang::SourceLocation loc) { if (!loc.isFileID()) return; // skip macro expansions etc locs.push_back(loc); }