Skip to content

Commit 2efe884

Browse files
committed
Find defines
1 parent e857065 commit 2efe884

12 files changed

+128
-1
lines changed

bindgen/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ add_executable(bindgen
2020
visitor/TreeVisitor.h
2121
visitor/TreeVisitor.cpp
2222
visitor/TreeConsumer.h
23+
defines/DefineFinderAction.h
24+
defines/DefineFinderAction.cpp
25+
defines/DefineFinder.cpp
26+
defines/DefineFinder.h
27+
defines/DefineFinderActionFactory.cpp
28+
defines/DefineFinderActionFactory.h
29+
ir/Define.h
30+
ir/Define.cpp
2331
TypeTranslator.h
2432
TypeTranslator.cpp
2533
HeaderManager.h

bindgen/Main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "defines/DefineFinderActionFactory.h"
12
#include "visitor/ScalaFrontendActionFactory.h"
23
#include <clang/Tooling/CommonOptionsParser.h>
34

@@ -71,8 +72,11 @@ int main(int argc, char *argv[]) {
7172
locations.clear();
7273

7374
IR ir(libName, linkName, objectName, Package.getValue());
74-
ScalaFrontendActionFactory actionFactory(ir);
7575

76+
DefineFinderActionFactory defineFinderActionFactory(ir);
77+
Tool.run(&defineFinderActionFactory);
78+
79+
ScalaFrontendActionFactory actionFactory(ir);
7680
int result = Tool.run(&actionFactory);
7781

7882
auto printLoc = PrintHeadersLocation.getValue();

bindgen/defines/DefineFinder.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "DefineFinder.h"
2+
3+
#include <clang/Basic/IdentifierTable.h>
4+
#include <clang/Lex/MacroInfo.h>
5+
6+
DefineFinder::DefineFinder(IR &ir, const clang::CompilerInstance &compiler)
7+
: ir(ir), compiler(compiler) {}
8+
9+
void DefineFinder::MacroDefined(const clang::Token &MacroNameTok,
10+
const clang::MacroDirective *MD) {
11+
std::string name = MacroNameTok.getIdentifierInfo()->getName();
12+
clang::SourceManager &sm = compiler.getSourceManager();
13+
if (sm.isWrittenInMainFile(MacroNameTok.getLocation())) {
14+
llvm::errs() << name << "\n";
15+
for (const auto &tok : MD->getMacroInfo()->tokens()) {
16+
llvm::errs() << tok.getName() << "\n";
17+
}
18+
}
19+
}

bindgen/defines/DefineFinder.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_DEFINEFINDER_H
2+
#define SCALA_NATIVE_BINDGEN_DEFINEFINDER_H
3+
4+
#include "../ir/IR.h"
5+
#include <clang/Frontend/CompilerInstance.h>
6+
#include <clang/Lex/PPCallbacks.h>
7+
8+
class DefineFinder : public clang::PPCallbacks {
9+
public:
10+
DefineFinder(IR &ir, const clang::CompilerInstance &compiler);
11+
12+
void MacroDefined(const clang::Token &MacroNameTok,
13+
const clang::MacroDirective *MD) override;
14+
15+
private:
16+
IR &ir;
17+
const clang::CompilerInstance &compiler;
18+
};
19+
20+
#endif // SCALA_NATIVE_BINDGEN_DEFINEFINDER_H
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "DefineFinderAction.h"
2+
3+
DefineFinderAction::DefineFinderAction(IR &ir) : ir(ir) {}

bindgen/defines/DefineFinderAction.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_DEFINEFINDERACTION_H
2+
#define SCALA_NATIVE_BINDGEN_DEFINEFINDERACTION_H
3+
4+
#include "DefineFinder.h"
5+
#include <clang/Frontend/FrontendActions.h>
6+
#include <clang/Lex/Preprocessor.h>
7+
8+
class DefineFinderAction : public clang::PreprocessOnlyAction {
9+
public:
10+
explicit DefineFinderAction(IR &ir);
11+
12+
protected:
13+
void ExecuteAction() override {
14+
getCompilerInstance().getPreprocessor().addPPCallbacks(
15+
std::unique_ptr<clang::PPCallbacks>(
16+
new DefineFinder(ir, getCompilerInstance())));
17+
18+
clang::PreprocessOnlyAction::ExecuteAction();
19+
}
20+
21+
private:
22+
IR &ir;
23+
};
24+
25+
#endif // SCALA_NATIVE_BINDGEN_DEFINEFINDERACTION_H
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "DefineFinderActionFactory.h"
2+
#include "DefineFinderAction.h"
3+
4+
DefineFinderActionFactory::DefineFinderActionFactory(IR &ir) : ir(ir) {}
5+
6+
clang::FrontendAction *DefineFinderActionFactory::create() {
7+
return new DefineFinderAction(ir);
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_DEFINEFINDERACTIONFACTORY_H
2+
#define SCALA_NATIVE_BINDGEN_DEFINEFINDERACTIONFACTORY_H
3+
4+
#include "../ir/IR.h"
5+
#include <clang/Tooling/Tooling.h>
6+
7+
class DefineFinderActionFactory : public clang::tooling::FrontendActionFactory {
8+
public:
9+
explicit DefineFinderActionFactory(IR &ir);
10+
11+
clang::FrontendAction *create() override;
12+
13+
private:
14+
IR &ir;
15+
};
16+
17+
#endif // SCALA_NATIVE_BINDGEN_DEFINEFINDERACTIONFACTORY_H

bindgen/ir/Define.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "Define.h"
2+
3+
Define::Define(const std::string &name, const std::string &type)
4+
: TypeAndName(name, type) {}

bindgen/ir/Define.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_DEFINE_H
2+
#define SCALA_NATIVE_BINDGEN_DEFINE_H
3+
4+
#include "TypeAndName.h"
5+
6+
class Define : TypeAndName {
7+
public:
8+
Define(const std::string &name, const std::string &type);
9+
};
10+
11+
#endif // SCALA_NATIVE_BINDGEN_DEFINE_H

0 commit comments

Comments
 (0)