clang  3.9.0
Builtins.cpp
Go to the documentation of this file.
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 
14 #include "clang/Basic/Builtins.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "llvm/ADT/StringRef.h"
19 using namespace clang;
20 
21 static const Builtin::Info BuiltinInfo[] = {
22  { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
23 #define BUILTIN(ID, TYPE, ATTRS) \
24  { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
25 #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
26  { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
27 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
28  { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
29 #include "clang/Basic/Builtins.def"
30 };
31 
32 const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
33  if (ID < Builtin::FirstTSBuiltin)
34  return BuiltinInfo[ID];
35  assert(((ID - Builtin::FirstTSBuiltin) <
36  (TSRecords.size() + AuxTSRecords.size())) &&
37  "Invalid builtin ID!");
38  if (isAuxBuiltinID(ID))
39  return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
40  return TSRecords[ID - Builtin::FirstTSBuiltin];
41 }
42 
44  const TargetInfo *AuxTarget) {
45  assert(TSRecords.empty() && "Already initialized target?");
46  TSRecords = Target.getTargetBuiltins();
47  if (AuxTarget)
48  AuxTSRecords = AuxTarget->getTargetBuiltins();
49 }
50 
52  StringRef FuncName(Name);
53  for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i)
54  if (FuncName.equals(BuiltinInfo[i].Name))
55  return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
56 
57  return false;
58 }
59 
60 bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
61  const LangOptions &LangOpts) {
62  bool BuiltinsUnsupported =
63  (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) &&
64  strchr(BuiltinInfo.Attributes, 'f');
65  bool MathBuiltinsUnsupported =
66  LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
67  llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
68  bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
69  bool MSModeUnsupported =
70  !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
71  bool ObjCUnsupported = !LangOpts.ObjC1 && BuiltinInfo.Langs == OBJC_LANG;
72  bool OclCUnsupported = LangOpts.OpenCLVersion != 200 &&
73  BuiltinInfo.Langs == OCLC20_LANG;
74  return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported &&
75  !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
76 }
77 
78 /// initializeBuiltins - Mark the identifiers for all the builtins with their
79 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
80 /// such.
82  const LangOptions& LangOpts) {
83  // Step #1: mark all target-independent builtins with their ID's.
84  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
85  if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
86  Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
87  }
88 
89  // Step #2: Register target-specific builtins.
90  for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
91  if (builtinIsSupported(TSRecords[i], LangOpts))
92  Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
93 
94  // Step #3: Register target-specific builtins for AuxTarget.
95  for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
96  Table.get(AuxTSRecords[i].Name)
97  .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
98 }
99 
101  Table.get(getRecord(ID).Name).setBuiltinID(0);
102 }
103 
104 bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
105  bool &HasVAListArg, const char *Fmt) const {
106  assert(Fmt && "Not passed a format string");
107  assert(::strlen(Fmt) == 2 &&
108  "Format string needs to be two characters long");
109  assert(::toupper(Fmt[0]) == Fmt[1] &&
110  "Format string is not in the form \"xX\"");
111 
112  const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
113  if (!Like)
114  return false;
115 
116  HasVAListArg = (*Like == Fmt[1]);
117 
118  ++Like;
119  assert(*Like == ':' && "Format specifier must be followed by a ':'");
120  ++Like;
121 
122  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
123  FormatIdx = ::strtol(Like, nullptr, 10);
124  return true;
125 }
126 
127 bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
128  bool &HasVAListArg) {
129  return isLike(ID, FormatIdx, HasVAListArg, "pP");
130 }
131 
132 bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
133  bool &HasVAListArg) {
134  return isLike(ID, FormatIdx, HasVAListArg, "sS");
135 }
const char * Attributes
Definition: Builtins.h:54
static const Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:21
static bool isBuiltinFunc(const char *Name)
Returns true if this is a libc/libm function without the '__builtin_' prefix.
Definition: Builtins.cpp:51
void forgetBuiltin(unsigned ID, IdentifierTable &Table)
Completely forget that the given ID was ever considered a builtin, e.g., because the user provided a ...
Definition: Builtins.cpp:100
bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like printf in its formatting rules and, if so, set the index to th...
Definition: Builtins.cpp:127
const char * Name
Definition: Builtins.h:54
bool isAuxBuiltinID(unsigned ID) const
Return true if builtin ID belongs to AuxTarget.
Definition: Builtins.h:193
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
bool isNoBuiltinFunc(const char *Name) const
Is this a libc/libm function that is no longer recognized as a builtin because a -fno-builtin-* optio...
Definition: LangOptions.cpp:39
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
LanguageID Langs
Definition: Builtins.h:55
Exposes information about the current target.
Defines the clang::LangOptions interface.
Implements an efficient mapping from strings to IdentifierInfo nodes.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
const char * HeaderName
Definition: Builtins.h:54
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
const std::string ID
void initializeBuiltins(IdentifierTable &Table, const LangOptions &LangOpts)
Mark the identifiers for all the builtins with their appropriate builtin ID # and mark any non-portab...
Definition: Builtins.cpp:81
unsigned getAuxBuiltinID(unsigned ID) const
Return real buitin ID (i.e.
Definition: Builtins.h:199
virtual ArrayRef< Builtin::Info > getTargetBuiltins() const =0
Return information about target-specific builtins for the current primary target, and info about whic...
bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like scanf in its formatting rules and, if so, set the index to the...
Definition: Builtins.cpp:132
void setBuiltinID(unsigned ID)
Defines the clang::TargetInfo interface.
void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget)
Perform target-specific initialization.
Definition: Builtins.cpp:43
Defines enum values for all the target-independent builtin functions.