|
| 1 | +#include "DefineFinder.h" |
| 2 | + |
| 3 | +#include <llvm/ADT/APInt.h> |
| 4 | +#include <sstream> |
| 5 | + |
| 6 | +DefineFinder::DefineFinder(IR &ir, const clang::CompilerInstance &compiler, |
| 7 | + clang::Preprocessor &pp) |
| 8 | + : ir(ir), compiler(compiler), pp(pp) {} |
| 9 | + |
| 10 | +void DefineFinder::MacroDefined(const clang::Token ¯oNameTok, |
| 11 | + const clang::MacroDirective *md) { |
| 12 | + clang::SourceManager &sm = compiler.getSourceManager(); |
| 13 | + if (sm.isWrittenInMainFile(macroNameTok.getLocation()) && md->isDefined() && |
| 14 | + !md->getMacroInfo()->isFunctionLike()) { |
| 15 | + /* save defines only from the given header. |
| 16 | + * |
| 17 | + * Ignore function-like definitions because they usually are meaningful |
| 18 | + * only inside C functions. */ |
| 19 | + |
| 20 | + std::vector<clang::Token> *tokens = expandDefine(*md); |
| 21 | + if (!tokens) { // there was function-like macro |
| 22 | + return; |
| 23 | + } |
| 24 | + std::string macroName = macroNameTok.getIdentifierInfo()->getName(); |
| 25 | + |
| 26 | + if (tokens->size() == 1 && |
| 27 | + (*tokens)[0].getKind() == clang::tok::numeric_constant) { |
| 28 | + clang::Token numberToken = (*tokens)[0]; |
| 29 | + std::string literal(numberToken.getLiteralData(), |
| 30 | + numberToken.getLength()); |
| 31 | + addNumericConstantDefine(macroName, literal, numberToken); |
| 32 | + } else if (tokens->size() == 2 && |
| 33 | + (*tokens)[0].getKind() == clang::tok::minus && |
| 34 | + (*tokens)[1].getKind() == clang::tok::numeric_constant) { |
| 35 | + clang::Token numberToken = (*tokens)[1]; |
| 36 | + std::string literal(numberToken.getLiteralData(), |
| 37 | + numberToken.getLength()); |
| 38 | + addNumericConstantDefine(macroName, literal, numberToken, false); |
| 39 | + } else if (tokens->size() == 1 && |
| 40 | + (*tokens)[0].getKind() == clang::tok::string_literal) { |
| 41 | + clang::Token stringToken = (*tokens)[0]; |
| 42 | + std::string literal(stringToken.getLiteralData(), |
| 43 | + stringToken.getLength()); |
| 44 | + ir.addLiteralDefine(macroName, "c" + literal, "native.CString"); |
| 45 | + } |
| 46 | + std::free(tokens); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @return array of tokens. Non of the returned tokens is a macro. |
| 52 | + */ |
| 53 | +std::vector<clang::Token> * |
| 54 | +DefineFinder::expandDefine(const clang::MacroDirective &md) { |
| 55 | + auto *expandedTokens = new std::vector<clang::Token>(); |
| 56 | + clang::ArrayRef<clang::Token> tokens = md.getMacroInfo()->tokens(); |
| 57 | + for (const auto &token : tokens) { |
| 58 | + if (isMacro(token)) { |
| 59 | + if (isFunctionLikeMacro(token)) { |
| 60 | + /* function-like macros are unsupported */ |
| 61 | + return nullptr; |
| 62 | + } |
| 63 | + std::vector<clang::Token> *newTokens = expandDefine( |
| 64 | + *pp.getLocalMacroDirective(token.getIdentifierInfo())); |
| 65 | + if (!newTokens) { |
| 66 | + std::free(expandedTokens); |
| 67 | + return nullptr; |
| 68 | + } |
| 69 | + for (const auto &newToken : *newTokens) { |
| 70 | + (*expandedTokens).push_back(newToken); |
| 71 | + } |
| 72 | + std::free(newTokens); |
| 73 | + } else { |
| 74 | + (*expandedTokens).push_back(token); |
| 75 | + } |
| 76 | + } |
| 77 | + return expandedTokens; |
| 78 | +} |
| 79 | + |
| 80 | +bool DefineFinder::isMacro(const clang::Token &token) { |
| 81 | + return token.isAnyIdentifier() && |
| 82 | + token.getIdentifierInfo()->hasMacroDefinition(); |
| 83 | +} |
| 84 | + |
| 85 | +bool DefineFinder::isFunctionLikeMacro(const clang::Token &token) { |
| 86 | + clang::MacroDirective *md = |
| 87 | + pp.getLocalMacroDirective(token.getIdentifierInfo()); |
| 88 | + return md->getMacroInfo()->isFunctionLike(); |
| 89 | +} |
| 90 | + |
| 91 | +void DefineFinder::MacroUndefined(const clang::Token ¯oNameTok, |
| 92 | + const clang::MacroDefinition &md, |
| 93 | + const clang::MacroDirective *undef) { |
| 94 | + clang::SourceManager &sm = compiler.getSourceManager(); |
| 95 | + if (sm.isWrittenInMainFile(macroNameTok.getLocation()) && |
| 96 | + md.getMacroInfo() && !md.getMacroInfo()->isFunctionLike()) { |
| 97 | + std::string macroName = macroNameTok.getIdentifierInfo()->getName(); |
| 98 | + ir.removeDefine(macroName); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void DefineFinder::addNumericConstantDefine(const std::string ¯oName, |
| 103 | + std::string literal, |
| 104 | + const clang::Token &token, |
| 105 | + bool positive) { |
| 106 | + clang::NumericLiteralParser parser(literal, token.getLocation(), pp); |
| 107 | + std::string type; |
| 108 | + std::string scalaLiteral; |
| 109 | + if (parser.isIntegerLiteral()) { |
| 110 | + if (parser.isLongLong) { |
| 111 | + /* literal has `LL` ending. `long long` is represented as `Long` |
| 112 | + * in Scala Native */ |
| 113 | + type = "native.CLongLong"; |
| 114 | + |
| 115 | + /* must fit into Scala integer type */ |
| 116 | + if (getTypeOfIntegerLiteral(parser, literal, positive).empty()) { |
| 117 | + type = ""; |
| 118 | + } |
| 119 | + } else if (parser.isLong) { |
| 120 | + /* literal has `L` ending */ |
| 121 | + type = "native.CLong"; |
| 122 | + |
| 123 | + /* must fit into Scala integer type */ |
| 124 | + if (getTypeOfIntegerLiteral(parser, literal, positive).empty()) { |
| 125 | + type = ""; |
| 126 | + } |
| 127 | + } else { |
| 128 | + type = getTypeOfIntegerLiteral(parser, literal, positive); |
| 129 | + } |
| 130 | + |
| 131 | + if (!type.empty()) { |
| 132 | + scalaLiteral = getDecimalLiteral(parser); |
| 133 | + if (type == "native.CLong" || type == "native.CLongLong") { |
| 134 | + scalaLiteral = scalaLiteral + "L"; |
| 135 | + } |
| 136 | + } |
| 137 | + } else if (parser.isFloatingLiteral()) { |
| 138 | + if (fitsIntoDouble(parser)) { |
| 139 | + type = "native.CDouble"; |
| 140 | + scalaLiteral = getDoubleLiteral(parser); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (!type.empty()) { |
| 145 | + if (!positive) { |
| 146 | + scalaLiteral = "-" + scalaLiteral; |
| 147 | + } |
| 148 | + ir.addLiteralDefine(macroName, scalaLiteral, type); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +std::string |
| 153 | +DefineFinder::getTypeOfIntegerLiteral(const clang::NumericLiteralParser &parser, |
| 154 | + const std::string &literal, |
| 155 | + bool positive) { |
| 156 | + |
| 157 | + if (integerFitsIntoType<int, uint>(parser, positive)) { |
| 158 | + return "native.CInt"; |
| 159 | + } else if (integerFitsIntoType<long, ulong>(parser, positive)) { |
| 160 | + return "native.CLong"; |
| 161 | + } else { |
| 162 | + llvm::errs() << "Warning: integer value does not fit into 8 bytes: " |
| 163 | + << literal << "\n"; |
| 164 | + llvm::errs().flush(); |
| 165 | + /** |
| 166 | + * `long long` value has mostly the same size as `long`. |
| 167 | + * Moreover in Scala Native the type is represented as `Long`: |
| 168 | + * @code |
| 169 | + * type CLongLong = Long |
| 170 | + * @endcode |
| 171 | + * Therefore the case of `long long` is not considered here. |
| 172 | + */ |
| 173 | + return ""; |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +template <typename signedT, typename unsignedT> |
| 178 | +bool DefineFinder::integerFitsIntoType(clang::NumericLiteralParser parser, |
| 179 | + bool positive) { |
| 180 | + /* absolute value of minimum negative number will not fit |
| 181 | + * into (sizeof(signedT) * 8 - 1) bits */ |
| 182 | + llvm::APInt uintValue(sizeof(signedT) * 8, 0, false); |
| 183 | + if (parser.GetIntegerValue(uintValue)) { |
| 184 | + /* absolute value of the number does not fit into (sizeof(signedT) * 8) |
| 185 | + * bits */ |
| 186 | + return false; |
| 187 | + } |
| 188 | + auto uval = static_cast<unsignedT>(uintValue.getZExtValue()); |
| 189 | + if (positive) { |
| 190 | + return uval <= |
| 191 | + static_cast<unsignedT>(std::numeric_limits<signedT>::max()); |
| 192 | + } else { |
| 193 | + return uval <= |
| 194 | + static_cast<unsignedT>(-std::numeric_limits<signedT>::min()); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +std::string |
| 199 | +DefineFinder::getDecimalLiteral(clang::NumericLiteralParser parser) { |
| 200 | + llvm::APInt val(8 * 8, 0, false); |
| 201 | + parser.GetIntegerValue(val); |
| 202 | + return std::to_string(val.getZExtValue()); |
| 203 | +} |
| 204 | + |
| 205 | +std::string DefineFinder::getDoubleLiteral(clang::NumericLiteralParser parser) { |
| 206 | + llvm::APFloat val(.0); // double |
| 207 | + parser.GetFloatValue(val); |
| 208 | + std::ostringstream ss; |
| 209 | + ss << val.convertToDouble(); |
| 210 | + return ss.str(); |
| 211 | +} |
| 212 | + |
| 213 | +bool DefineFinder::fitsIntoDouble(clang::NumericLiteralParser parser) { |
| 214 | + llvm::APFloat val(.0); // double |
| 215 | + parser.GetFloatValue(val); |
| 216 | + std::ostringstream ss; |
| 217 | + ss << val.convertToDouble(); |
| 218 | + return ss.str() != "inf"; |
| 219 | +} |
0 commit comments