clang 22.0.0git
LangOptions.cpp
Go to the documentation of this file.
1//===- LangOptions.cpp - C Language Family Language Options ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the LangOptions class.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/Support/Path.h"
16
17using namespace clang;
18
20#define LANGOPT(Name, Bits, Default, Compatibility, Description) Name = Default;
21#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
22 set##Name(Default);
23#include "clang/Basic/LangOptions.def"
24}
25
27#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
28 if constexpr (CompatibilityKind::Compatibility == CompatibilityKind::Benign) \
29 Name = Default;
30#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
31 if constexpr (CompatibilityKind::Compatibility == CompatibilityKind::Benign) \
32 Name = static_cast<unsigned>(Default);
33#include "clang/Basic/LangOptions.def"
34
35 // Reset "benign" options with implied values (Options.td ImpliedBy relations)
36 // rather than their defaults. This avoids unexpected combinations and
37 // invocations that cannot be round-tripped to arguments.
38 // FIXME: we should derive this automatically from ImpliedBy in tablegen.
39 AllowFPReassoc = UnsafeFPMath;
40 NoHonorInfs = FastMath;
41 NoHonorNaNs = FastMath;
42
43 // These options do not affect AST generation.
44 NoSanitizeFiles.clear();
47
48 CurrentModule.clear();
49 IsHeaderFile = false;
50}
51
52bool LangOptions::isNoBuiltinFunc(StringRef FuncName) const {
53 for (unsigned i = 0, e = NoBuiltinFuncs.size(); i != e; ++i)
54 if (FuncName == NoBuiltinFuncs[i])
55 return true;
56 return false;
57}
58
60 const int Ver = OpenCLCPlusPlus ? OpenCLCPlusPlusVersion : OpenCLVersion;
61 if (OpenCLCPlusPlus && Ver != 100)
62 return VersionTuple(Ver / 100);
63 return VersionTuple(Ver / 100, (Ver % 100) / 10);
64}
65
67 if (!OpenCLCPlusPlus)
68 return OpenCLVersion;
69 if (OpenCLCPlusPlusVersion == 100)
70 return 200;
71 if (OpenCLCPlusPlusVersion == 202100)
72 return 300;
73 llvm_unreachable("Unknown OpenCL version");
74}
75
77 for (const auto &Entry : MacroPrefixMap)
78 if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
79 break;
80}
81
83 std::string Result;
84 {
85 llvm::raw_string_ostream Out(Result);
86 Out << (OpenCLCPlusPlus ? "C++ for OpenCL" : "OpenCL C") << " version "
87 << getOpenCLVersionTuple().getAsString();
88 }
89 return Result;
90}
91
93 const llvm::Triple &T,
94 std::vector<std::string> &Includes,
96 // Set some properties which depend solely on the input kind; it would be nice
97 // to move these to the language standard, and have the driver resolve the
98 // input kind + language standard.
99 //
100 // FIXME: Perhaps a better model would be for a single source file to have
101 // multiple language standards (C / C++ std, ObjC std, OpenCL std, OpenMP std)
102 // simultaneously active?
103 if (Lang == Language::Asm) {
104 Opts.AsmPreprocessor = 1;
105 } else if (Lang == Language::ObjC || Lang == Language::ObjCXX) {
106 Opts.ObjC = 1;
107 }
108
112 Opts.LangStd = LangStd;
113 Opts.LineComment = Std.hasLineComments();
114 Opts.C99 = Std.isC99();
115 Opts.C11 = Std.isC11();
116 Opts.C17 = Std.isC17();
117 Opts.C23 = Std.isC23();
118 Opts.C2y = Std.isC2y();
119 Opts.CPlusPlus = Std.isCPlusPlus();
120 Opts.CPlusPlus11 = Std.isCPlusPlus11();
121 Opts.CPlusPlus14 = Std.isCPlusPlus14();
122 Opts.CPlusPlus17 = Std.isCPlusPlus17();
123 Opts.CPlusPlus20 = Std.isCPlusPlus20();
124 Opts.CPlusPlus23 = Std.isCPlusPlus23();
125 Opts.CPlusPlus26 = Std.isCPlusPlus26();
126 Opts.GNUMode = Std.isGNUMode();
127 Opts.GNUCVersion = 0;
128 Opts.HexFloats = Std.hasHexFloats();
129 Opts.WChar = Std.isCPlusPlus();
130 Opts.Digraphs = Std.hasDigraphs();
131 Opts.RawStringLiterals = Std.hasRawStringLiterals();
132 Opts.NamedLoops = Std.isC2y();
133
134 Opts.HLSL = Lang == Language::HLSL;
135 if (Opts.HLSL) {
136 if (Opts.IncludeDefaultHeader)
137 Includes.push_back("hlsl.h");
138 // Set maximum matrix dimension to 4 for HLSL
139 Opts.MaxMatrixDimension = 4;
140 }
141
142 // Set OpenCL Version.
143 Opts.OpenCL = Std.isOpenCL();
144 if (LangStd == LangStandard::lang_opencl10)
145 Opts.OpenCLVersion = 100;
146 else if (LangStd == LangStandard::lang_opencl11)
147 Opts.OpenCLVersion = 110;
148 else if (LangStd == LangStandard::lang_opencl12)
149 Opts.OpenCLVersion = 120;
150 else if (LangStd == LangStandard::lang_opencl20)
151 Opts.OpenCLVersion = 200;
152 else if (LangStd == LangStandard::lang_opencl30)
153 Opts.OpenCLVersion = 300;
154 else if (LangStd == LangStandard::lang_openclcpp10)
155 Opts.OpenCLCPlusPlusVersion = 100;
156 else if (LangStd == LangStandard::lang_openclcpp2021)
157 Opts.OpenCLCPlusPlusVersion = 202100;
158 else if (LangStd == LangStandard::lang_hlsl2015)
159 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2015;
160 else if (LangStd == LangStandard::lang_hlsl2016)
161 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2016;
162 else if (LangStd == LangStandard::lang_hlsl2017)
163 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2017;
164 else if (LangStd == LangStandard::lang_hlsl2018)
165 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2018;
166 else if (LangStd == LangStandard::lang_hlsl2021)
167 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2021;
168 else if (LangStd == LangStandard::lang_hlsl202x)
169 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_202x;
170 else if (LangStd == LangStandard::lang_hlsl202y)
171 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_202y;
172
173 // OpenCL has some additional defaults.
174 if (Opts.OpenCL) {
175 Opts.AltiVec = 0;
176 Opts.ZVector = 0;
177 Opts.setDefaultFPContractMode(LangOptions::FPM_On);
178 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
179 Opts.OpenCLPipes = Opts.getOpenCLCompatibleVersion() == 200;
180 Opts.OpenCLGenericAddressSpace = Opts.getOpenCLCompatibleVersion() == 200;
181
182 // Include default header file for OpenCL.
183 if (Opts.IncludeDefaultHeader) {
184 if (Opts.DeclareOpenCLBuiltins) {
185 // Only include base header file for builtin types and constants.
186 Includes.push_back("opencl-c-base.h");
187 } else {
188 Includes.push_back("opencl-c.h");
189 }
190 }
191 }
192
193 Opts.HIP = Lang == Language::HIP;
194 Opts.CUDA = Lang == Language::CUDA || Opts.HIP;
195 if (Opts.HIP) {
196 // HIP toolchain does not support 'Fast' FPOpFusion in backends since it
197 // fuses multiplication/addition instructions without contract flag from
198 // device library functions in LLVM bitcode, which causes accuracy loss in
199 // certain math functions, e.g. tan(-1e20) becomes -0.933 instead of 0.8446.
200 // For device library functions in bitcode to work, 'Strict' or 'Standard'
201 // FPOpFusion options in backends is needed. Therefore 'fast-honor-pragmas'
202 // FP contract option is used to allow fuse across statements in frontend
203 // whereas respecting contract flag in backend.
204 Opts.setDefaultFPContractMode(LangOptions::FPM_FastHonorPragmas);
205 } else if (Opts.CUDA) {
206 if (T.isSPIRV()) {
207 // Emit OpenCL version metadata in LLVM IR when targeting SPIR-V.
208 Opts.OpenCLVersion = 200;
209 }
210 // Allow fuse across statements disregarding pragmas.
211 Opts.setDefaultFPContractMode(LangOptions::FPM_Fast);
212 }
213
214 // OpenCL, C++ and C23 have bool, true, false keywords.
215 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus || Opts.C23;
216
217 // OpenCL and HLSL have half keyword
218 Opts.Half = Opts.OpenCL || Opts.HLSL;
219
220 Opts.PreserveVec3Type = Opts.HLSL;
221}
222
224 FPOptions result(LO);
225 return result;
226}
227
228FPOptionsOverride FPOptions::getChangesSlow(const FPOptions &Base) const {
229 FPOptions::storage_type OverrideMask = 0;
230#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
231 if (get##NAME() != Base.get##NAME()) \
232 OverrideMask |= NAME##Mask;
233#include "clang/Basic/FPOptions.def"
234 return FPOptionsOverride(*this, OverrideMask);
235}
236
237LLVM_DUMP_METHOD void FPOptions::dump() {
238#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
239 llvm::errs() << "\n " #NAME " " << get##NAME();
240#include "clang/Basic/FPOptions.def"
241 llvm::errs() << "\n";
242}
243
244LLVM_DUMP_METHOD void FPOptionsOverride::dump() {
245#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
246 if (has##NAME##Override()) \
247 llvm::errs() << "\n " #NAME " Override is " << get##NAME##Override();
248#include "clang/Basic/FPOptions.def"
249 llvm::errs() << "\n";
250}
251
252std::optional<uint32_t> LangOptions::getCPlusPlusLangStd() const {
253 if (!CPlusPlus)
254 return std::nullopt;
255
257 if (CPlusPlus26)
258 Std = LangStandard::lang_cxx26;
259 else if (CPlusPlus23)
260 Std = LangStandard::lang_cxx23;
261 else if (CPlusPlus20)
262 Std = LangStandard::lang_cxx20;
263 else if (CPlusPlus17)
264 Std = LangStandard::lang_cxx17;
265 else if (CPlusPlus14)
266 Std = LangStandard::lang_cxx14;
267 else if (CPlusPlus11)
268 Std = LangStandard::lang_cxx11;
269 else
270 Std = LangStandard::lang_cxx98;
271
273}
274
275std::optional<uint32_t> LangOptions::getCLangStd() const {
277 if (C2y)
278 Std = LangStandard::lang_c2y;
279 else if (C23)
280 Std = LangStandard::lang_c23;
281 else if (C17)
282 Std = LangStandard::lang_c17;
283 else if (C11)
284 Std = LangStandard::lang_c11;
285 else if (C99)
286 Std = LangStandard::lang_c99;
287 else if (!GNUMode && Digraphs)
288 Std = LangStandard::lang_c94;
289 else
290 return std::nullopt;
291
293}
Defines the clang::LangOptions interface.
Represents difference between two FPOptions values.
LLVM_DUMP_METHOD void dump()
static FPOptions defaultWithoutTrailingStorage(const LangOptions &LO)
Return the default value of FPOptions that's used when trailing storage isn't required.
uint32_t storage_type
LLVM_DUMP_METHOD void dump()
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
void resetNonModularOptions()
Reset all of the options that are not considered when building a module.
std::vector< std::string > NoBuiltinFuncs
A list of all -fno-builtin-* function names (e.g., memset).
std::vector< std::string > XRayNeverInstrumentFiles
Paths to the XRay "never instrument" files specifying which objects (files, functions,...
bool isNoBuiltinFunc(StringRef Name) const
Is this a libc/libm function that is no longer recognized as a builtin because a -fno-builtin-* optio...
std::string getOpenCLVersionString() const
Return the OpenCL C or C++ for OpenCL language name and version as a string.
bool IsHeaderFile
Indicates whether the front-end is explicitly told that the input is a header file (i....
std::vector< std::string > XRayAlwaysInstrumentFiles
Paths to the XRay "always instrument" files specifying which objects (files, functions,...
VersionTuple getOpenCLVersionTuple() const
Return the OpenCL C or C++ version as a VersionTuple.
static void setLangDefaults(LangOptions &Opts, Language Lang, const llvm::Triple &T, std::vector< std::string > &Includes, LangStandard::Kind LangStd=LangStandard::lang_unspecified)
Set language defaults for the given input language and language standard in the given LangOptions obj...
std::map< std::string, std::string, std::greater< std::string > > MacroPrefixMap
A prefix map for FILE, BASE_FILE and __builtin_FILE().
void remapPathPrefix(SmallVectorImpl< char > &Path) const
Remap path prefix according to -fmacro-prefix-path option.
std::optional< uint32_t > getCPlusPlusLangStd() const
Returns the most applicable C++ standard-compliant language version code.
LangStandard::Kind LangStd
The used language standard.
std::optional< uint32_t > getCLangStd() const
Returns the most applicable C standard-compliant language version code.
unsigned getOpenCLCompatibleVersion() const
Return the OpenCL version that kernel language is compatible with.
std::vector< std::string > NoSanitizeFiles
Paths to files specifying which objects (files, functions, variables) should not be instrumented.
std::string CurrentModule
The name of the current module, of which the main source file is a part.
The JSON file list parser is used to communicate input to InstallAPI.
@ CPlusPlus23
@ CPlusPlus20
@ CPlusPlus
@ CPlusPlus11
@ CPlusPlus14
@ CPlusPlus26
@ CPlusPlus17
LangStandard::Kind getDefaultLanguageStandard(clang::Language Lang, const llvm::Triple &T)
Language
The language for the input, used to select and validate the language standard and possible actions.
@ Asm
Assembly: we accept this only so that we can preprocess it.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
const FunctionProtoType * T
LangStandard - Information about the properties of a particular language standard.
bool isCPlusPlus20() const
isCPlusPlus20 - Language is a C++20 variant (or later).
bool isCPlusPlus() const
isCPlusPlus - Language is a C++ variant.
bool hasRawStringLiterals() const
hasRawStringLiterals - Language supports R"()" raw string literals.
bool hasHexFloats() const
hasHexFloats - Language supports hexadecimal float constants.
bool isC11() const
isC11 - Language is a superset of C11.
bool isCPlusPlus17() const
isCPlusPlus17 - Language is a C++17 variant (or later).
bool hasDigraphs() const
hasDigraphs - Language supports digraphs.
bool isCPlusPlus14() const
isCPlusPlus14 - Language is a C++14 variant (or later).
bool isC17() const
isC17 - Language is a superset of C17.
bool isCPlusPlus26() const
isCPlusPlus26 - Language is a post-C++26 variant (or later).
bool isC2y() const
isC2y - Language is a superset of C2y.
bool isCPlusPlus11() const
isCPlusPlus11 - Language is a C++11 variant (or later).
std::optional< uint32_t > getVersion() const
Get the version code for this language standard.
bool isC23() const
isC23 - Language is a superset of C23.
static const LangStandard & getLangStandardForKind(Kind K)
bool isOpenCL() const
isOpenCL - Language is a OpenCL variant.
bool hasLineComments() const
Language supports '//' comments.
bool isCPlusPlus23() const
isCPlusPlus23 - Language is a post-C++23 variant (or later).
bool isGNUMode() const
isGNUMode - Language includes GNU extensions.
bool isC99() const
isC99 - Language is a superset of C99.