some update.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
#include <clang/Lex/PPCallbacks.h>
|
||||
#include <clang/Lex/MacroArgs.h>
|
||||
|
||||
using namespace clang;
|
||||
|
||||
class PPCallback : public clang::PPCallbacks {
|
||||
private:
|
||||
clang::Preprocessor& pp;
|
||||
@@ -10,44 +12,57 @@ private:
|
||||
public:
|
||||
PPCallback(clang::Preprocessor& pp) : pp(pp), sm(pp.getSourceManager()) {}
|
||||
|
||||
void MacroExpands(const clang::Token& token,
|
||||
const clang::MacroDefinition& macro,
|
||||
clang::SourceRange range,
|
||||
const clang::MacroArgs* args) override {
|
||||
std::string name = pp.getSpelling(token);
|
||||
if(name.starts_with("_"))
|
||||
return;
|
||||
|
||||
clang::MacroInfo* info = macro.getMacroInfo();
|
||||
// info->isBuiltinMacro();
|
||||
// info->isFunctionLike();
|
||||
// info->isObjectLike();
|
||||
// info->isVariadic();
|
||||
// info->getNumParams();
|
||||
// info->params();
|
||||
|
||||
const int size = args->getNumMacroArguments(); // Expanding macro arguments
|
||||
for(auto i = 0; i < size; ++i) {
|
||||
// get first token of first argument of expanding macro
|
||||
const clang::Token* first = args->getUnexpArgument(i);
|
||||
// iterate over tokens of first argument of expanding macro
|
||||
for(auto j = 0; j < args->getArgLength(first); ++j) {
|
||||
const clang::Token& tok = *(first + j);
|
||||
// llvm::outs() << "Arg: " << pp.getSpelling(tok) << "\n";
|
||||
}
|
||||
void InclusionDirective(SourceLocation HashLoc,
|
||||
const Token& IncludeTok,
|
||||
StringRef FileName,
|
||||
bool IsAngled,
|
||||
CharSourceRange FilenameRange,
|
||||
OptionalFileEntryRef File,
|
||||
StringRef SearchPath,
|
||||
StringRef RelativePath,
|
||||
const Module* SuggestedModule,
|
||||
bool ModuleImported,
|
||||
SrcMgr::CharacteristicKind FileType) override {
|
||||
if(sm.isInMainFile(HashLoc)) {
|
||||
HashLoc.dump(sm);
|
||||
IncludeTok.getLocation().dump(sm);
|
||||
IncludeTok.getEndLoc().dump(sm);
|
||||
}
|
||||
|
||||
auto expandingRange = sm.getExpansionRange(range);
|
||||
auto text = clang::Lexer::getSourceText(expandingRange, sm, pp.getLangOpts());
|
||||
llvm::outs() << text << "\n";
|
||||
}
|
||||
|
||||
void MacroDefined(const clang::Token& token, const clang::MacroDirective* directive) override {
|
||||
std::string name = pp.getSpelling(token);
|
||||
if(name.starts_with("_"))
|
||||
return;
|
||||
llvm::outs() << "MacroDefined: " << name << "\n";
|
||||
}
|
||||
// void MacroExpands(const clang::Token& token,
|
||||
// const clang::MacroDefinition& macro,
|
||||
// clang::SourceRange range,
|
||||
// const clang::MacroArgs* args) override {
|
||||
// std::string name = pp.getSpelling(token);
|
||||
// if(name.starts_with("_"))
|
||||
// return;
|
||||
//
|
||||
// clang::MacroInfo* info = macro.getMacroInfo();
|
||||
// // info->isBuiltinMacro();
|
||||
// // info->isFunctionLike();
|
||||
// // info->isObjectLike();
|
||||
// // info->isVariadic();
|
||||
// // info->getNumParams();
|
||||
// // info->params();
|
||||
//
|
||||
// const int size = args->getNumMacroArguments(); // Expanding macro arguments
|
||||
// for(auto i = 0; i < size; ++i) {
|
||||
// // get first token of first argument of expanding macro
|
||||
// const clang::Token* first = args->getUnexpArgument(i);
|
||||
// // iterate over tokens of first argument of expanding macro
|
||||
// for(auto j = 0; j < args->getArgLength(first); ++j) {
|
||||
// const clang::Token& tok = *(first + j);
|
||||
// // llvm::outs() << "Arg: " << pp.getSpelling(tok) << "\n";
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// auto expandingRange = sm.getExpansionRange(range);
|
||||
// auto text = clang::Lexer::getSourceText(expandingRange, sm, pp.getLangOpts());
|
||||
// llvm::outs() << text << "\n";
|
||||
//}
|
||||
|
||||
void MacroDefined(const clang::Token& token, const clang::MacroDirective* directive) override {}
|
||||
};
|
||||
|
||||
int main(int argc, const char** argv) {
|
||||
@@ -64,7 +79,7 @@ int main(int argc, const char** argv) {
|
||||
|
||||
auto invocation = std::make_shared<clang::CompilerInvocation>();
|
||||
std::vector<const char*> args = {
|
||||
"clang++",
|
||||
"/usr/local/bin/clang++",
|
||||
"-Xclang",
|
||||
"-no-round-trip-args",
|
||||
"-std=c++20",
|
||||
@@ -107,7 +122,7 @@ int main(int argc, const char** argv) {
|
||||
// // // TODO: split annoated token
|
||||
// // }
|
||||
// });
|
||||
// pp.addPPCallbacks(std::make_unique<PPCallback>(pp));
|
||||
pp.addPPCallbacks(std::make_unique<PPCallback>(pp));
|
||||
clang::syntax::TokenCollector collector{pp};
|
||||
|
||||
if(auto error = action.Execute()) {
|
||||
@@ -119,15 +134,18 @@ int main(int argc, const char** argv) {
|
||||
buffer.dumpForTests();
|
||||
auto tokens = buffer.spelledTokens(sm.getMainFileID());
|
||||
for(auto& token: tokens) {
|
||||
llvm::outs() << "Token: " << token.text(sm) << "\n";
|
||||
|
||||
llvm::outs() << "Token: " << token.text(sm) << " " << clang::tok::getTokenName(token.kind())
|
||||
<< "\n";
|
||||
}
|
||||
auto tokens2 = buffer.expandedTokens();
|
||||
for(auto& token: tokens2) {
|
||||
llvm::outs() << "Token: " << token.text(sm) << "\n";
|
||||
}
|
||||
// auto buffer = sm.getBufferData(sm.getMainFileID());
|
||||
// llvm::outs() << buffer << "\n";
|
||||
// buffer.spelledTokenContaining()
|
||||
// all operations should before action end
|
||||
buffer.expansionStartingAt(tokens[0].location());
|
||||
// auto tokens2 = buffer.expandedTokens();
|
||||
// for(auto& token: tokens2) {
|
||||
// token.dumpForTests(sm);
|
||||
// }
|
||||
// auto buffer = sm.getBufferData(sm.getMainFileID());
|
||||
// llvm::outs() << buffer << "\n";
|
||||
// buffer.spelledTokenContaining()
|
||||
// all operations should before action end
|
||||
action.EndSourceFile();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user