| 49 | } |
| 50 | |
| 51 | std::vector<std::string> |
| 52 | SourceAnalyzerImpl::getIncludes(const ASTUnit &AST) const { |
| 53 | std::vector<std::string> Includes; |
| 54 | auto &ASTContext = AST.getASTContext(); |
| 55 | auto &SM = *const_cast<SourceManager *>(&ASTContext.getSourceManager()); |
| 56 | FileID FID = SM.getMainFileID(); |
| 57 | Rewriter TheRewriter; |
| 58 | TheRewriter.setSourceMgr(SM, ASTContext.getLangOpts()); |
| 59 | |
| 60 | auto DumpSLocEntry = [&](const SrcMgr::SLocEntry &Entry) { |
| 61 | if (!Entry.isFile()) |
| 62 | return; |
| 63 | |
| 64 | auto &FI = Entry.getFile(); |
| 65 | |
| 66 | SourceLocation IncludeLoc = FI.getIncludeLoc(); |
| 67 | if (!IncludeLoc.isValid() || !SM.isInFileID(IncludeLoc, FID)) |
| 68 | return; |
| 69 | |
| 70 | int Offset = 0; |
| 71 | clang::SourceLocation EndLoc; |
| 72 | while (true) { |
| 73 | EndLoc = IncludeLoc.getLocWithOffset(Offset); |
| 74 | std::string Str = TheRewriter.getRewrittenText(SourceRange(EndLoc)); |
| 75 | assert(!Str.empty() && "Unexpected Program State"); |
| 76 | |
| 77 | auto Front = Str.front(); |
| 78 | if (Front == '\"' || Front == '>') |
| 79 | break; |
| 80 | |
| 81 | Offset += Str.size(); |
| 82 | } |
| 83 | clang::SourceRange headerSourceRange(FI.getIncludeLoc(), EndLoc); |
| 84 | Includes.push_back(TheRewriter.getRewrittenText(headerSourceRange)); |
| 85 | }; |
| 86 | |
| 87 | // Dump local SLocEntries. |
| 88 | for (unsigned ID = 0, NumIDs = SM.local_sloc_entry_size(); ID != NumIDs; ++ID) |
| 89 | DumpSLocEntry(SM.getLocalSLocEntry(ID)); |
| 90 | |
| 91 | // Dump loaded SLocEntries. |
| 92 | for (unsigned Index = 0; Index != SM.loaded_sloc_entry_size(); ++Index) { |
| 93 | if (!SM.getLoadedSLocEntry(Index).isFile()) |
| 94 | continue; |
| 95 | |
| 96 | DumpSLocEntry(SM.getLoadedSLocEntry(Index)); |
| 97 | } |
| 98 | |
| 99 | // The sequence of Included Header List from FileInfo.getIncludeLoc is |
| 100 | // reverse. So, Reverse again to get correct sequence of the list. |
| 101 | std::reverse(Includes.begin(), Includes.end()); |
| 102 | return Includes; |
| 103 | } |
| 104 | |
| 105 | const clang::FunctionDecl * |
| 106 | SourceAnalyzerImpl::getMainFuncDecl(const clang::ASTUnit &U) const { |