clang  15.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 
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/Path.h"
16 
17 using namespace clang;
18 
19 LangOptions::LangOptions() : LangStd(LangStandard::lang_unspecified) {
20 #define LANGOPT(Name, Bits, Default, Description) Name = Default;
21 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) set##Name(Default);
22 #include "clang/Basic/LangOptions.def"
23 }
24 
26 #define LANGOPT(Name, Bits, Default, Description)
27 #define BENIGN_LANGOPT(Name, Bits, Default, Description) Name = Default;
28 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
29  Name = static_cast<unsigned>(Default);
30 #include "clang/Basic/LangOptions.def"
31 
32  // These options do not affect AST generation.
33  NoSanitizeFiles.clear();
36 
37  CurrentModule.clear();
38  IsHeaderFile = false;
39 }
40 
41 bool LangOptions::isNoBuiltinFunc(StringRef FuncName) const {
42  for (unsigned i = 0, e = NoBuiltinFuncs.size(); i != e; ++i)
43  if (FuncName.equals(NoBuiltinFuncs[i]))
44  return true;
45  return false;
46 }
47 
48 VersionTuple LangOptions::getOpenCLVersionTuple() const {
49  const int Ver = OpenCLCPlusPlus ? OpenCLCPlusPlusVersion : OpenCLVersion;
50  if (OpenCLCPlusPlus && Ver != 100)
51  return VersionTuple(Ver / 100);
52  return VersionTuple(Ver / 100, (Ver % 100) / 10);
53 }
54 
56  if (!OpenCLCPlusPlus)
57  return OpenCLVersion;
58  if (OpenCLCPlusPlusVersion == 100)
59  return 200;
60  if (OpenCLCPlusPlusVersion == 202100)
61  return 300;
62  llvm_unreachable("Unknown OpenCL version");
63 }
64 
66  for (const auto &Entry : MacroPrefixMap)
67  if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
68  break;
69 }
70 
72  std::string Result;
73  {
74  llvm::raw_string_ostream Out(Result);
75  Out << (OpenCLCPlusPlus ? "C++ for OpenCL" : "OpenCL C") << " version "
76  << getOpenCLVersionTuple().getAsString();
77  }
78  return Result;
79 }
80 
82  const llvm::Triple &T,
83  std::vector<std::string> &Includes,
84  LangStandard::Kind LangStd) {
85  // Set some properties which depend solely on the input kind; it would be nice
86  // to move these to the language standard, and have the driver resolve the
87  // input kind + language standard.
88  //
89  // FIXME: Perhaps a better model would be for a single source file to have
90  // multiple language standards (C / C++ std, ObjC std, OpenCL std, OpenMP std)
91  // simultaneously active?
92  if (Lang == Language::Asm) {
93  Opts.AsmPreprocessor = 1;
94  } else if (Lang == Language::ObjC || Lang == Language::ObjCXX) {
95  Opts.ObjC = 1;
96  }
97 
101  Opts.LangStd = LangStd;
102  Opts.LineComment = Std.hasLineComments();
103  Opts.C99 = Std.isC99();
104  Opts.C11 = Std.isC11();
105  Opts.C17 = Std.isC17();
106  Opts.C2x = Std.isC2x();
107  Opts.CPlusPlus = Std.isCPlusPlus();
108  Opts.CPlusPlus11 = Std.isCPlusPlus11();
109  Opts.CPlusPlus14 = Std.isCPlusPlus14();
110  Opts.CPlusPlus17 = Std.isCPlusPlus17();
111  Opts.CPlusPlus20 = Std.isCPlusPlus20();
112  Opts.CPlusPlus2b = Std.isCPlusPlus2b();
113  Opts.GNUMode = Std.isGNUMode();
114  Opts.GNUCVersion = 0;
115  Opts.HexFloats = Std.hasHexFloats();
116  Opts.WChar = Std.isCPlusPlus();
117  Opts.Digraphs = Std.hasDigraphs();
118 
119  Opts.HLSL = Lang == Language::HLSL;
120 
121  // Set OpenCL Version.
122  Opts.OpenCL = Std.isOpenCL();
123  if (LangStd == LangStandard::lang_opencl10)
124  Opts.OpenCLVersion = 100;
125  else if (LangStd == LangStandard::lang_opencl11)
126  Opts.OpenCLVersion = 110;
127  else if (LangStd == LangStandard::lang_opencl12)
128  Opts.OpenCLVersion = 120;
129  else if (LangStd == LangStandard::lang_opencl20)
130  Opts.OpenCLVersion = 200;
131  else if (LangStd == LangStandard::lang_opencl30)
132  Opts.OpenCLVersion = 300;
133  else if (LangStd == LangStandard::lang_openclcpp10)
134  Opts.OpenCLCPlusPlusVersion = 100;
135  else if (LangStd == LangStandard::lang_openclcpp2021)
136  Opts.OpenCLCPlusPlusVersion = 202100;
137  else if (LangStd == LangStandard::lang_hlsl2015)
138  Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2015;
139  else if (LangStd == LangStandard::lang_hlsl2016)
140  Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2016;
141  else if (LangStd == LangStandard::lang_hlsl2017)
142  Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2017;
143  else if (LangStd == LangStandard::lang_hlsl2018)
144  Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2018;
145  else if (LangStd == LangStandard::lang_hlsl2021)
146  Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2021;
147  else if (LangStd == LangStandard::lang_hlsl202x)
148  Opts.HLSLVersion = (unsigned)LangOptions::HLSL_202x;
149 
150  // OpenCL has some additional defaults.
151  if (Opts.OpenCL) {
152  Opts.AltiVec = 0;
153  Opts.ZVector = 0;
154  Opts.setDefaultFPContractMode(LangOptions::FPM_On);
155  Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
156  Opts.OpenCLPipes = Opts.getOpenCLCompatibleVersion() == 200;
157  Opts.OpenCLGenericAddressSpace = Opts.getOpenCLCompatibleVersion() == 200;
158 
159  // Include default header file for OpenCL.
160  if (Opts.IncludeDefaultHeader) {
161  if (Opts.DeclareOpenCLBuiltins) {
162  // Only include base header file for builtin types and constants.
163  Includes.push_back("opencl-c-base.h");
164  } else {
165  Includes.push_back("opencl-c.h");
166  }
167  }
168  }
169 
170  Opts.HIP = Lang == Language::HIP;
171  Opts.CUDA = Lang == Language::CUDA || Opts.HIP;
172  if (Opts.HIP) {
173  // HIP toolchain does not support 'Fast' FPOpFusion in backends since it
174  // fuses multiplication/addition instructions without contract flag from
175  // device library functions in LLVM bitcode, which causes accuracy loss in
176  // certain math functions, e.g. tan(-1e20) becomes -0.933 instead of 0.8446.
177  // For device library functions in bitcode to work, 'Strict' or 'Standard'
178  // FPOpFusion options in backends is needed. Therefore 'fast-honor-pragmas'
179  // FP contract option is used to allow fuse across statements in frontend
180  // whereas respecting contract flag in backend.
181  Opts.setDefaultFPContractMode(LangOptions::FPM_FastHonorPragmas);
182  } else if (Opts.CUDA) {
183  if (T.isSPIRV()) {
184  // Emit OpenCL version metadata in LLVM IR when targeting SPIR-V.
185  Opts.OpenCLVersion = 200;
186  }
187  // Allow fuse across statements disregarding pragmas.
188  Opts.setDefaultFPContractMode(LangOptions::FPM_Fast);
189  }
190 
191  Opts.RenderScript = Lang == Language::RenderScript;
192 
193  // OpenCL, C++ and C2x have bool, true, false keywords.
194  Opts.Bool = Opts.OpenCL || Opts.CPlusPlus || Opts.C2x;
195 
196  // OpenCL has half keyword
197  Opts.Half = Opts.OpenCL;
198 }
199 
201  FPOptions result(LO);
202  return result;
203 }
204 
205 LLVM_DUMP_METHOD void FPOptions::dump() {
206 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
207  llvm::errs() << "\n " #NAME " " << get##NAME();
208 #include "clang/Basic/FPOptions.def"
209  llvm::errs() << "\n";
210 }
211 
212 LLVM_DUMP_METHOD void FPOptionsOverride::dump() {
213 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
214  if (has##NAME##Override()) \
215  llvm::errs() << "\n " #NAME " Override is " << get##NAME##Override();
216 #include "clang/Basic/FPOptions.def"
217  llvm::errs() << "\n";
218 }
clang::Language::CUDA
@ CUDA
clang::LangOptions::CurrentModule
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:410
clang::LangOptions::HLSL_2021
@ HLSL_2021
Definition: LangOptions.h:169
clang::LangOptions::XRayAlwaysInstrumentFiles
std::vector< std::string > XRayAlwaysInstrumentFiles
Paths to the XRay "always instrument" files specifying which objects (files, functions,...
Definition: LangOptions.h:373
clang::LangOptions::getOpenCLCompatibleVersion
unsigned getOpenCLCompatibleVersion() const
Return the OpenCL version that kernel language is compatible with.
Definition: LangOptions.cpp:55
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::LangOptions::HLSL_2018
@ HLSL_2018
Definition: LangOptions.h:168
clang::LangOptions::FPM_On
@ FPM_On
Definition: LangOptions.h:252
clang::Language::Asm
@ Asm
Assembly: we accept this only so that we can preprocess it.
clang::LangOptions::getOpenCLVersionString
std::string getOpenCLVersionString() const
Return the OpenCL C or C++ for OpenCL language name and version as a string.
Definition: LangOptions.cpp:71
clang::LangOptions::MacroPrefixMap
std::map< std::string, std::string, std::greater< std::string > > MacroPrefixMap
A prefix map for FILE, BASE_FILE and __builtin_FILE().
Definition: LangOptions.h:425
clang::LangOptions::HLSL_2017
@ HLSL_2017
Definition: LangOptions.h:167
clang::LangStandard::Kind
Kind
Definition: LangStandard.h:69
clang::Language::RenderScript
@ RenderScript
LangOptions.h
Std
LangStandard::Kind Std
Definition: InterpolatingCompilationDatabase.cpp:134
clang::LangOptions::HLSL_2015
@ HLSL_2015
Definition: LangOptions.h:165
clang::FPOptions
Definition: LangOptions.h:593
clang::LangOptions::NoBuiltinFuncs
std::vector< std::string > NoBuiltinFuncs
A list of all -fno-builtin-* function names (e.g., memset).
Definition: LangOptions.h:422
clang::LangOptions::IsHeaderFile
bool IsHeaderFile
Indicates whether the front-end is explicitly told that the input is a header file (i....
Definition: LangOptions.h:447
clang::FPOptions::defaultWithoutTrailingStorage
static FPOptions defaultWithoutTrailingStorage(const LangOptions &LO)
Return the default value of FPOptions that's used when trailing storage isn't required.
Definition: LangOptions.cpp:200
clang::LangOptions::isNoBuiltinFunc
bool isNoBuiltinFunc(StringRef Name) const
Is this a libc/libm function that is no longer recognized as a builtin because a -fno-builtin-* optio...
Definition: LangOptions.cpp:41
clang::LangOptions::XRayNeverInstrumentFiles
std::vector< std::string > XRayNeverInstrumentFiles
Paths to the XRay "never instrument" files specifying which objects (files, functions,...
Definition: LangOptions.h:379
clang::LangOptions::HLSL_202x
@ HLSL_202x
Definition: LangOptions.h:170
clang::LangOptions::LangOptions
LangOptions()
Definition: LangOptions.cpp:19
clang::LangOptions::getOpenCLVersionTuple
VersionTuple getOpenCLVersionTuple() const
Return the OpenCL C or C++ version as a VersionTuple.
Definition: LangOptions.cpp:48
clang::Language
Language
The language for the input, used to select and validate the language standard and possible actions.
Definition: LangStandard.h:23
clang::LangOptions::NoSanitizeFiles
std::vector< std::string > NoSanitizeFiles
Paths to files specifying which objects (files, functions, variables) should not be instrumented.
Definition: LangOptions.h:367
clang::Language::ObjCXX
@ ObjCXX
clang::LangOptions::setLangDefaults
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...
Definition: LangOptions.cpp:81
clang::Language::ObjC
@ ObjC
clang::LangOptions::FPM_FastHonorPragmas
@ FPM_FastHonorPragmas
Definition: LangOptions.h:258
clang::LangStandard::lang_unspecified
@ lang_unspecified
Definition: LangStandard.h:73
clang::LangStandard
LangStandard - Information about the properties of a particular language standard.
Definition: LangStandard.h:68
clang::LangOptions::LangStd
LangStandard::Kind LangStd
The used language standard.
Definition: LangOptions.h:358
clang::FPOptionsOverride::dump
LLVM_DUMP_METHOD void dump()
Definition: LangOptions.cpp:212
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:78
clang
Definition: CalledOnceCheck.h:17
clang::LangOptions::HLSL_2016
@ HLSL_2016
Definition: LangOptions.h:166
clang::LangOptions::resetNonModularOptions
void resetNonModularOptions()
Reset all of the options that are not considered when building a module.
Definition: LangOptions.cpp:25
unsigned
clang::getDefaultLanguageStandard
LangStandard::Kind getDefaultLanguageStandard(clang::Language Lang, const llvm::Triple &T)
Definition: LangStandards.cpp:47
clang::LangOptions::remapPathPrefix
void remapPathPrefix(SmallVectorImpl< char > &Path) const
Remap path prefix according to -fmacro-prefix-path option.
Definition: LangOptions.cpp:65
clang::Language::HIP
@ HIP
llvm::SmallVectorImpl< char >
clang::HLSL
@ HLSL
Definition: LangStandard.h:63
clang::FPOptions::dump
LLVM_DUMP_METHOD void dump()
Definition: LangOptions.cpp:205
clang::LangOptions::FPM_Fast
@ FPM_Fast
Definition: LangOptions.h:255
clang::LangStandard::getLangStandardForKind
static const LangStandard & getLangStandardForKind(Kind K)
Definition: LangStandards.cpp:20