string Utils::filenameForLoc(SourceLocation loc, const clang::SourceManager &sm) { string filename = sm.getFilename(loc); auto splitted = clazy_std::splitString(filename, '/'); if (splitted.empty()) return {}; return splitted[splitted.size() - 1]; }
void EditTracker::Add(const clang::SourceManager& source_manager, clang::SourceLocation location, llvm::StringRef original_text, llvm::StringRef new_text) { llvm::StringRef filename; for (int i = 0; i < 10; i++) { filename = source_manager.getFilename(location); if (!filename.empty() || !location.isMacroID()) break; // Otherwise, no filename and the SourceLocation is a macro ID. Look one // level up the stack... location = source_manager.getImmediateMacroCallerLoc(location); } assert(!filename.empty() && "Can't track edit with no filename!"); auto result = tracked_edits_.try_emplace(original_text); if (result.second) { result.first->getValue().new_text = new_text; } result.first->getValue().filenames.try_emplace(filename); }
core::annotations::Location convertClangSrcLoc(core::NodeManager& man, const clang::SourceManager& sm, clang::SourceLocation start, clang::SourceLocation end) { // check file validity FileID&& fileId = sm.getFileID(start); if(fileId.isInvalid()) return core::annotations::Location::getShared(); const clang::FileEntry* fileEntry = sm.getFileEntryForID(fileId); // if we cannot get the file entry, lets try to get the source filename directly std::string filename; if(!fileEntry) { StringRef s = sm.getFilename(start); filename = s.str(); } else { assert_true(fileEntry); filename = fileEntry->getName(); } // update macro locations, if required start = getExpansionLoc(sm, start); end = getExpansionLoc(sm, end); // generate location object core::IRBuilder builder(man); return core::annotations::Location(builder.stringValue(filename.c_str()), core::annotations::TextPosition(sm.getSpellingLineNumber(start), sm.getSpellingColumnNumber(start)), core::annotations::TextPosition(sm.getSpellingLineNumber(end), sm.getSpellingColumnNumber(end))); }