-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCppBundler.cpp
125 lines (107 loc) · 3.87 KB
/
CppBundler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Tooling/Tooling.h"
class CppBundlerCallback : public clang::PPCallbacks {
public:
CppBundlerCallback(clang::Rewriter &R) : TheRewriter(R) {
InclusionSourceRanges.push_back({});
}
void InclusionDirective(clang::SourceLocation HashLoc,
const clang::Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
clang::CharSourceRange FilenameRange,
#if LLVM_VERSION_MAJOR >= 16
clang::OptionalFileEntryRef File,
#elif LLVM_VERSION_MAJOR >= 15
llvm::Optional<clang::FileEntryRef> File,
#else
const clang::FileEntry *File,
#endif
llvm::StringRef SearchPath,
llvm::StringRef RelativePath,
const clang::Module *Imported,
clang::SrcMgr::CharacteristicKind FileType) final {
if (FileType != clang::SrcMgr::C_User) {
return;
}
InclusionSourceRanges.emplace_back(HashLoc, FilenameRange.getEnd());
}
void FileSkipped(const clang::FileEntryRef &SkippedFile,
const clang::Token &FilenameTok,
clang::SrcMgr::CharacteristicKind FileType) final {
if (FileType != clang::SrcMgr::C_User) {
return;
}
TheRewriter.RemoveText(InclusionSourceRanges.back());
InclusionSourceRanges.pop_back();
}
void FileChanged(clang::SourceLocation Loc,
clang::PPCallbacks::FileChangeReason Reason,
clang::SrcMgr::CharacteristicKind FileType,
clang::FileID PrevFID) final {
if (FileType != clang::SrcMgr::C_User) {
return;
}
if (Reason != clang::PPCallbacks::ExitFile) {
return;
}
auto &SM = TheRewriter.getSourceMgr();
if (SM.isWrittenInBuiltinFile(Loc)) {
return;
}
auto IncludeFileLoc = SM.getLocForStartOfFile(PrevFID);
if (IncludeFileLoc.isInvalid()) {
return;
}
if (SM.getFileCharacteristic(IncludeFileLoc) != clang::SrcMgr::C_User) {
return;
}
const auto &IncludedBuffer = TheRewriter.getEditBuffer(PrevFID);
std::string IncludedContent;
llvm::raw_string_ostream IncludedStream(IncludedContent);
IncludedBuffer.write(IncludedStream).flush();
TheRewriter.ReplaceText(InclusionSourceRanges.back(), IncludedContent);
InclusionSourceRanges.pop_back();
}
private:
clang::Rewriter &TheRewriter;
llvm::SmallVector<clang::SourceRange, 32> InclusionSourceRanges;
};
class CppBundlerAction : public clang::PreprocessOnlyAction {
protected:
virtual void ExecuteAction() {
const auto &CI = getCompilerInstance();
auto &SM = CI.getSourceManager();
auto &PP = CI.getPreprocessor();
clang::Rewriter TheRewriter;
TheRewriter.setSourceMgr(SM, CI.getLangOpts());
PP.addPPCallbacks(std::make_unique<CppBundlerCallback>(TheRewriter));
clang::PreprocessOnlyAction::ExecuteAction();
TheRewriter.getEditBuffer(SM.getMainFileID()).write(llvm::outs());
}
};
static inline void printHelpMessage() {
llvm::outs() << "Usage: cpp-bundle FILE [OPTIONS]...\n";
}
int main(int argc, const char *argv[]) {
if (argc <= 1) {
printHelpMessage();
return 0;
}
std::vector<std::string> Sources, Args;
Sources.emplace_back(argv[1]);
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--help") {
printHelpMessage();
return 0;
} else if (i >= 2) {
Args.emplace_back(argv[i]);
}
}
clang::tooling::FixedCompilationDatabase CompileDb(".", std::move(Args));
clang::tooling::ClangTool Tool(CompileDb, Sources);
return Tool.run(
clang::tooling::newFrontendActionFactory<CppBundlerAction>().get());
}