xref: /freebsd/contrib/llvm-project/clang/lib/Basic/LangOptions.cpp (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 
13 #include "clang/Basic/LangOptions.h"
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 
25 void LangOptions::resetNonModularOptions() {
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();
34   XRayAlwaysInstrumentFiles.clear();
35   XRayNeverInstrumentFiles.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 
55 unsigned LangOptions::getOpenCLCompatibleVersion() const {
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 
65 void LangOptions::remapPathPrefix(SmallString<256> &Path) const {
66   for (const auto &Entry : MacroPrefixMap)
67     if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
68       break;
69 }
70 
71 std::string LangOptions::getOpenCLVersionString() const {
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 
81 FPOptions FPOptions::defaultWithoutTrailingStorage(const LangOptions &LO) {
82   FPOptions result(LO);
83   return result;
84 }
85 
86 LLVM_DUMP_METHOD void FPOptions::dump() {
87 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS)                                    \
88   llvm::errs() << "\n " #NAME " " << get##NAME();
89 #include "clang/Basic/FPOptions.def"
90   llvm::errs() << "\n";
91 }
92 
93 LLVM_DUMP_METHOD void FPOptionsOverride::dump() {
94 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS)                                    \
95   if (has##NAME##Override())                                                   \
96     llvm::errs() << "\n " #NAME " Override is " << get##NAME##Override();
97 #include "clang/Basic/FPOptions.def"
98   llvm::errs() << "\n";
99 }
100