|
| 1 | + |
| 2 | +#ifndef COREF_CFAMILY_SRC_EXTRACTOR_COREFASTCONSUMER_HPP |
| 3 | +#define COREF_CFAMILY_SRC_EXTRACTOR_COREFASTCONSUMER_HPP |
| 4 | + |
| 5 | +#include "ASTUtil.hpp" |
| 6 | +#include "CorefASTVisitor.hpp" |
| 7 | +#include <clang/Tooling/Tooling.h> |
| 8 | +#include <llvm/Support/Regex.h> |
| 9 | + |
| 10 | +using namespace llvm; |
| 11 | + |
| 12 | +namespace coref { |
| 13 | + |
| 14 | +class CorefASTConsumer : public clang::ASTConsumer { |
| 15 | + private: |
| 16 | + const CorefUri _corefUri; |
| 17 | + std::set<clang::FileID> _visitedFileIds; |
| 18 | + std::unique_ptr<llvm::Regex> _blacklistDirFilter; |
| 19 | + |
| 20 | + inline bool isInBlackListDir(StringRef absolutePath) { |
| 21 | + return _blacklistDirFilter && _blacklistDirFilter->match(absolutePath); |
| 22 | + } |
| 23 | + |
| 24 | + protected: |
| 25 | + /// An override HandleTranslationUnit |
| 26 | + /// This method is called when the ASTs for entire translation unit have |
| 27 | + /// been parsed. \param astContext |
| 28 | + void HandleTranslationUnit(clang::ASTContext &astContext) final { |
| 29 | + coref::StorageFacade::transaction([&]() mutable { |
| 30 | + // insert entry for Program table, entry could be existed already. |
| 31 | + auto programOid = CorefUri::generateCorpusOId(_corefUri.getCorpus()); |
| 32 | + coref::StorageFacade::insertClassObj(Program{programOid, _corefUri.getCorpus()}); |
| 33 | + |
| 34 | + std::unordered_map<CorefOid, File> newVisitFileMap{}; |
| 35 | + coref::CorefASTVisitor visitor(astContext, _corefUri, programOid, newVisitFileMap); |
| 36 | + |
| 37 | + auto decls = astContext.getTranslationUnitDecl()->decls(); |
| 38 | + auto &sourceMngr = astContext.getSourceManager(); |
| 39 | + for (auto &decl : decls) { |
| 40 | + auto curFileId = sourceMngr.getFileID(decl->getLocation()); |
| 41 | + |
| 42 | + if (_visitedFileIds.find(curFileId) != _visitedFileIds.end()) { |
| 43 | + // skip visited files |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + // skip AST nodes having invalid source location |
| 48 | + if (!decl->getLocation().isValid()) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + // todo: need to verify the accuracy of function |
| 53 | + // "isInSystemHeader" & "isInSystemMacro" |
| 54 | + if (sourceMngr.isInSystemHeader(decl->getLocation()) || |
| 55 | + sourceMngr.isInSystemMacro(decl->getLocation())) { |
| 56 | + // skip AST nodes in system headers |
| 57 | + _visitedFileIds.insert(curFileId); |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + StringRef absolutePath = sourceMngr.getFilename(decl->getLocation()); |
| 62 | + if (absolutePath.empty()) |
| 63 | + continue; |
| 64 | + if (isInBlackListDir(absolutePath)) { |
| 65 | + _visitedFileIds.insert(curFileId); |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + StringRef relativePath; |
| 70 | + getRootRelativePath(absolutePath, relativePath); |
| 71 | + // note: relativePath would be an absolute path when handling a |
| 72 | + // framework file. |
| 73 | + |
| 74 | + auto fileOid = |
| 75 | + CorefUri::generateFileOId(_corefUri.getCorpus(), std::string(relativePath)); |
| 76 | + if (coref::StorageFacade::checkFileObjExist(programOid, fileOid)) { |
| 77 | + // skip file that have been added in Sqlite DB |
| 78 | + _visitedFileIds.insert(curFileId); |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if (newVisitFileMap.find(fileOid) == newVisitFileMap.end()) { |
| 83 | + File file{fileOid, std::string(relativePath), |
| 84 | + std::string(sys::path::extension(relativePath)), |
| 85 | + std::string(sys::path::filename(relativePath)), programOid}; |
| 86 | + newVisitFileMap.insert({fileOid, std::move(file)}); |
| 87 | + } |
| 88 | + visitor.setExtractFileOid(fileOid); |
| 89 | + visitor.TraverseDecl(decl); |
| 90 | + } |
| 91 | + |
| 92 | + // traverse the newFileStruct and update the File table |
| 93 | + for (auto &[fileOid, f] : newVisitFileMap) { |
| 94 | + coref::StorageFacade::insertClassObj(std::move(f)); |
| 95 | + } |
| 96 | + |
| 97 | + return true; |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + public: |
| 102 | + CorefASTConsumer(const CorefUri &corefUri, std::vector<std::string> &blacklistDir) |
| 103 | + : _corefUri(corefUri) { |
| 104 | + std::stringstream regexStr; |
| 105 | + bool first = true; |
| 106 | + for (auto dir : blacklistDir) { |
| 107 | + if (first) { |
| 108 | + regexStr << "(" << dir << ")"; |
| 109 | + first = false; |
| 110 | + } else { |
| 111 | + regexStr << "|(" << dir << ")"; |
| 112 | + } |
| 113 | + } |
| 114 | + _blacklistDirFilter = std::make_unique<llvm::Regex>(regexStr.str()); |
| 115 | + _blacklistDirFilter->isValid(); |
| 116 | + }; |
| 117 | +}; |
| 118 | +} // namespace coref |
| 119 | + |
| 120 | +#endif // COREF_CFAMILY_SRC_EXTRACTOR_COREFASTCONSUMER_HPP |
0 commit comments