clang  3.7.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/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 using namespace clang;
21 
22 static const Builtin::Info BuiltinInfo[] = {
23  { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES},
24 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
25 #define LANGBUILTIN(ID, TYPE, ATTRS, BUILTIN_LANG) { #ID, TYPE, ATTRS, 0, BUILTIN_LANG },
26 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
27  BUILTIN_LANG },
28 #include "clang/Basic/Builtins.def"
29 };
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 
39  // Get the target specific builtins from the target.
40  TSRecords = nullptr;
41  NumTSRecords = 0;
42 }
43 
45  assert(NumTSRecords == 0 && "Already initialized target?");
46  Target.getTargetBuiltins(TSRecords, NumTSRecords);
47 }
48 
49 bool Builtin::Context::BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
50  const LangOptions &LangOpts) {
51  bool BuiltinsUnsupported = LangOpts.NoBuiltin &&
52  strchr(BuiltinInfo.Attributes, 'f');
53  bool MathBuiltinsUnsupported =
54  LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
55  llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
56  bool GnuModeUnsupported = !LangOpts.GNUMode &&
57  (BuiltinInfo.builtin_lang & GNU_LANG);
58  bool MSModeUnsupported = !LangOpts.MicrosoftExt &&
59  (BuiltinInfo.builtin_lang & MS_LANG);
60  bool ObjCUnsupported = !LangOpts.ObjC1 &&
61  BuiltinInfo.builtin_lang == OBJC_LANG;
62  return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
63  !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
64 }
65 
66 /// InitializeBuiltins - Mark the identifiers for all the builtins with their
67 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
68 /// such.
70  const LangOptions& LangOpts) {
71  // Step #1: mark all target-independent builtins with their ID's.
72  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
73  if (BuiltinIsSupported(BuiltinInfo[i], LangOpts)) {
74  Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
75  }
76 
77  // Step #2: Register target-specific builtins.
78  for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
79  if (BuiltinIsSupported(TSRecords[i], LangOpts))
80  Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
81 }
82 
83 void
85  // Final all target-independent names
86  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
87  if (!strchr(BuiltinInfo[i].Attributes, 'f'))
88  Names.push_back(BuiltinInfo[i].Name);
89 
90  // Find target-specific names.
91  for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
92  if (!strchr(TSRecords[i].Attributes, 'f'))
93  Names.push_back(TSRecords[i].Name);
94 }
95 
97  Table.get(GetRecord(ID).Name).setBuiltinID(0);
98 }
99 
100 bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
101  bool &HasVAListArg, const char *Fmt) const {
102  assert(Fmt && "Not passed a format string");
103  assert(::strlen(Fmt) == 2 &&
104  "Format string needs to be two characters long");
105  assert(::toupper(Fmt[0]) == Fmt[1] &&
106  "Format string is not in the form \"xX\"");
107 
108  const char *Like = ::strpbrk(GetRecord(ID).Attributes, Fmt);
109  if (!Like)
110  return false;
111 
112  HasVAListArg = (*Like == Fmt[1]);
113 
114  ++Like;
115  assert(*Like == ':' && "Format specifier must be followed by a ':'");
116  ++Like;
117 
118  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
119  FormatIdx = ::strtol(Like, nullptr, 10);
120  return true;
121 }
122 
123 bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
124  bool &HasVAListArg) {
125  return isLike(ID, FormatIdx, HasVAListArg, "pP");
126 }
127 
128 bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
129  bool &HasVAListArg) {
130  return isLike(ID, FormatIdx, HasVAListArg, "sS");
131 }
const char * Attributes
Definition: Builtins.h:53
static const Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:22
virtual void getTargetBuiltins(const Builtin::Info *&Records, unsigned &NumRecords) const =0
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:123
void GetBuiltinNames(SmallVectorImpl< const char * > &Names)
Populate the vector with the names of all of the builtins.
Definition: Builtins.cpp:84
LanguageID builtin_lang
Definition: Builtins.h:54
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
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:96
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
Exposes information about the current target.
Defines the clang::LangOptions interface.
Implements an efficient mapping from strings to IdentifierInfo nodes.
void InitializeTarget(const TargetInfo &Target)
Perform target-specific initialization.
Definition: Builtins.cpp:44
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
const char * HeaderName
Definition: Builtins.h:53
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
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:128
void setBuiltinID(unsigned 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:69
Defines the clang::TargetInfo interface.
Defines enum values for all the target-independent builtin functions.