Builtins.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===--- Builtins.cpp - Builtin function implementation -------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements various things for builtin functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/Builtins.h"
  14. #include "clang/Basic/IdentifierTable.h"
  15. #include "clang/Basic/LangOptions.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "llvm/ADT/StringRef.h"
  18. using namespace clang;
  19. static const Builtin::Info BuiltinInfo[] = {
  20. { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES},
  21. #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
  22. #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
  23. { #ID, TYPE, ATTRS, 0, LANGS } \
  24. ,
  25. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
  26. { #ID, TYPE, ATTRS, HEADER, LANGS } \
  27. ,
  28. #include "clang/Basic/Builtins.def"
  29. };
  30. const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
  31. if (ID < Builtin::FirstTSBuiltin)
  32. return BuiltinInfo[ID];
  33. assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
  34. return TSRecords[ID - Builtin::FirstTSBuiltin];
  35. }
  36. Builtin::Context::Context() {
  37. // Get the target specific builtins from the target.
  38. TSRecords = nullptr;
  39. NumTSRecords = 0;
  40. }
  41. void Builtin::Context::initializeTarget(const TargetInfo &Target) {
  42. assert(NumTSRecords == 0 && "Already initialized target?");
  43. Target.getTargetBuiltins(TSRecords, NumTSRecords);
  44. }
  45. bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
  46. const LangOptions &LangOpts) {
  47. bool BuiltinsUnsupported = LangOpts.NoBuiltin &&
  48. strchr(BuiltinInfo.Attributes, 'f');
  49. bool MathBuiltinsUnsupported =
  50. LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
  51. llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
  52. bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
  53. bool MSModeUnsupported =
  54. !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
  55. bool ObjCUnsupported = !LangOpts.ObjC1 && BuiltinInfo.Langs == OBJC_LANG;
  56. return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
  57. !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
  58. }
  59. /// InitializeBuiltins - Mark the identifiers for all the builtins with their
  60. /// appropriate builtin ID # and mark any non-portable builtin identifiers as
  61. /// such.
  62. void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
  63. const LangOptions& LangOpts) {
  64. // Step #1: mark all target-independent builtins with their ID's.
  65. for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
  66. if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
  67. Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
  68. }
  69. // Step #2: Register target-specific builtins.
  70. for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
  71. if (builtinIsSupported(TSRecords[i], LangOpts))
  72. Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
  73. }
  74. void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
  75. Table.get(getRecord(ID).Name).setBuiltinID(0);
  76. }
  77. bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
  78. bool &HasVAListArg, const char *Fmt) const {
  79. assert(Fmt && "Not passed a format string");
  80. assert(::strlen(Fmt) == 2 &&
  81. "Format string needs to be two characters long");
  82. assert(::toupper(Fmt[0]) == Fmt[1] &&
  83. "Format string is not in the form \"xX\"");
  84. const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
  85. if (!Like)
  86. return false;
  87. HasVAListArg = (*Like == Fmt[1]);
  88. ++Like;
  89. assert(*Like == ':' && "Format specifier must be followed by a ':'");
  90. ++Like;
  91. assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
  92. FormatIdx = ::strtol(Like, nullptr, 10);
  93. return true;
  94. }
  95. bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
  96. bool &HasVAListArg) {
  97. return isLike(ID, FormatIdx, HasVAListArg, "pP");
  98. }
  99. bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
  100. bool &HasVAListArg) {
  101. return isLike(ID, FormatIdx, HasVAListArg, "sS");
  102. }