Builtins.cpp 4.6 KB

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