xref: /llvm-project/clang/lib/Basic/Builtins.cpp (revision 9246b6830a468d9d0a0925eae168543b8d603e29)
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"
15 #include "clang/Basic/IdentifierTable.h"
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Basic/LangOptions.h"
18 using namespace clang;
19 
20 static const Builtin::Info BuiltinInfo[] = {
21   { "not a builtin function", 0, 0, 0, ALL_LANGUAGES, false },
22 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES, false },
23 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
24                                                             BUILTIN_LANG, false },
25 #include "clang/Basic/Builtins.def"
26 };
27 
28 const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
29   if (ID < Builtin::FirstTSBuiltin)
30     return BuiltinInfo[ID];
31   assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
32   return TSRecords[ID - Builtin::FirstTSBuiltin];
33 }
34 
35 Builtin::Context::Context(const TargetInfo &Target) {
36   // Get the target specific builtins from the target.
37   TSRecords = 0;
38   NumTSRecords = 0;
39   Target.getTargetBuiltins(TSRecords, NumTSRecords);
40 }
41 
42 /// InitializeBuiltins - Mark the identifiers for all the builtins with their
43 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
44 /// such.
45 void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
46                                           const LangOptions& LangOpts) {
47   // Step #1: mark all target-independent builtins with their ID's.
48   for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
49     if (!BuiltinInfo[i].Suppressed &&
50         (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f'))) {
51       if (LangOpts.ObjC1 ||
52           BuiltinInfo[i].builtin_lang != clang::OBJC_LANG)
53         Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
54     }
55 
56   // Step #2: Register target-specific builtins.
57   for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
58     if (!TSRecords[i].Suppressed &&
59         (!LangOpts.NoBuiltin ||
60          (TSRecords[i].Attributes &&
61           !strchr(TSRecords[i].Attributes, 'f'))))
62       Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
63 }
64 
65 void
66 Builtin::Context::GetBuiltinNames(llvm::SmallVectorImpl<const char *> &Names,
67                                   bool NoBuiltins) {
68   // Final all target-independent names
69   for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
70     if (!BuiltinInfo[i].Suppressed &&
71         (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')))
72       Names.push_back(BuiltinInfo[i].Name);
73 
74   // Find target-specific names.
75   for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
76     if (!TSRecords[i].Suppressed &&
77         (!NoBuiltins ||
78          (TSRecords[i].Attributes &&
79           !strchr(TSRecords[i].Attributes, 'f'))))
80       Names.push_back(TSRecords[i].Name);
81 }
82 
83 void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
84   Table.get(GetRecord(ID).Name).setBuiltinID(0);
85 }
86 
87 bool
88 Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
89                                bool &HasVAListArg) {
90   const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
91   if (!Printf)
92     return false;
93 
94   HasVAListArg = (*Printf == 'P');
95 
96   ++Printf;
97   assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
98   ++Printf;
99 
100   assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
101   FormatIdx = strtol(Printf, 0, 10);
102   return true;
103 }
104 
105 // FIXME: Refactor with isPrintfLike.
106 bool
107 Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
108                               bool &HasVAListArg) {
109   const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
110   if (!Scanf)
111     return false;
112 
113   HasVAListArg = (*Scanf == 'S');
114 
115   ++Scanf;
116   assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
117   ++Scanf;
118 
119   assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
120   FormatIdx = strtol(Scanf, 0, 10);
121   return true;
122 }
123 
124 
125