Skip to content

Commit abe03c4

Browse files
committed
Get type of numeric_constant
1 parent 4a2c905 commit abe03c4

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

bindgen/defines/DefineFinder.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include "DefineFinder.h"
22

33
#include <clang/Basic/IdentifierTable.h>
4+
#include <clang/Lex/LiteralSupport.h>
45
#include <clang/Lex/MacroInfo.h>
56

6-
DefineFinder::DefineFinder(IR &ir, const clang::CompilerInstance &compiler)
7-
: ir(ir), compiler(compiler) {}
7+
DefineFinder::DefineFinder(IR &ir, const clang::CompilerInstance &compiler,
8+
clang::Preprocessor &pp)
9+
: ir(ir), compiler(compiler), pp(pp) {}
810

911
void DefineFinder::MacroDefined(const clang::Token &MacroNameTok,
1012
const clang::MacroDirective *MD) {
@@ -38,14 +40,11 @@ void DefineFinder::MacroDefined(const clang::Token &MacroNameTok,
3840
/* might be converted directly to Scala code */
3941
std::string literal(finalToken->getLiteralData(),
4042
finalToken->getLength());
41-
switch (finalToken->getKind()) {
42-
case clang::tok::numeric_constant:
43-
ir.addLiteralDefine(macroName, literal);
44-
break;
45-
case clang::tok::string_literal:
43+
if (finalToken->getKind() == clang::tok::numeric_constant) {
44+
addNumericConstantDefine(macroName, literal, finalToken);
45+
} else if (finalToken->getKind() == clang::tok::string_literal) {
4646
ir.addLiteralDefine(macroName, "c" + literal, "native.CString");
47-
break;
48-
default:
47+
} else {
4948
llvm::errs() << "Warning: type of literal "
5049
<< finalToken->getName() << " is unsupported\n";
5150
llvm::errs().flush();
@@ -75,3 +74,28 @@ void DefineFinder::MacroUndefined(const clang::Token &MacroNameTok,
7574
ir.removeDefine(macroName);
7675
}
7776
}
77+
78+
void DefineFinder::addNumericConstantDefine(const std::string &macroName,
79+
const std::string &literal,
80+
const clang::Token *finalToken) {
81+
clang::NumericLiteralParser parser(literal, finalToken->getLocation(), pp);
82+
std::string type;
83+
if (parser.isIntegerLiteral()) {
84+
if (parser.isLongLong) {
85+
return;
86+
}
87+
if (parser.isLong) {
88+
type = "native.CLong";
89+
} else {
90+
type = "native.CInt";
91+
}
92+
} else {
93+
if (parser.isFloat) {
94+
type = "native.CFloat";
95+
// TODO: distinguish between float and double
96+
}
97+
}
98+
if (!type.empty()) {
99+
ir.addLiteralDefine(macroName, literal, type);
100+
}
101+
}

bindgen/defines/DefineFinder.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#include "../ir/IR.h"
55
#include <clang/Frontend/CompilerInstance.h>
66
#include <clang/Lex/PPCallbacks.h>
7+
#include <clang/Lex/Preprocessor.h>
78

89
class DefineFinder : public clang::PPCallbacks {
910
public:
10-
DefineFinder(IR &ir, const clang::CompilerInstance &compiler);
11+
DefineFinder(IR &ir, const clang::CompilerInstance &compiler,
12+
clang::Preprocessor &pp);
1113

1214
void MacroDefined(const clang::Token &MacroNameTok,
1315
const clang::MacroDirective *MD) override;
@@ -18,8 +20,13 @@ class DefineFinder : public clang::PPCallbacks {
1820
private:
1921
IR &ir;
2022
const clang::CompilerInstance &compiler;
23+
clang::Preprocessor &pp;
2124

2225
const clang::Token *getFinalIdentifier(const clang::Token &token) const;
26+
27+
void addNumericConstantDefine(const std::string &macroName,
28+
const std::string &literal,
29+
const clang::Token *finalToken);
2330
};
2431

2532
#endif // SCALA_NATIVE_BINDGEN_DEFINEFINDER_H

bindgen/defines/DefineFinderAction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ DefineFinderAction::DefineFinderAction(IR &ir) : ir(ir) {}
55
void DefineFinderAction::ExecuteAction() {
66
getCompilerInstance().getPreprocessor().addPPCallbacks(
77
std::unique_ptr<clang::PPCallbacks>(
8-
new DefineFinder(ir, getCompilerInstance())));
8+
new DefineFinder(ir, getCompilerInstance(),
9+
getCompilerInstance().getPreprocessor())));
910

1011
clang::PreprocessOnlyAction::ExecuteAction();
1112
}

0 commit comments

Comments
 (0)