Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/source-location.h @ 69:33d812a61356
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 17:55:14 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 69:33d812a61356 |
---|---|
1 // Copyright (c) 2021 Cloudflare, Inc. and contributors | |
2 // Licensed under the MIT License: | |
3 // | |
4 // Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 // of this software and associated documentation files (the "Software"), to deal | |
6 // in the Software without restriction, including without limitation the rights | |
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 // copies of the Software, and to permit persons to whom the Software is | |
9 // furnished to do so, subject to the following conditions: | |
10 // | |
11 // The above copyright notice and this permission notice shall be included in | |
12 // all copies or substantial portions of the Software. | |
13 // | |
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 // THE SOFTWARE. | |
21 | |
22 #pragma once | |
23 | |
24 #include "string.h" | |
25 | |
26 KJ_BEGIN_HEADER | |
27 | |
28 // GCC does not implement __builtin_COLUMN() as that's non-standard but MSVC & clang do. | |
29 // MSVC does as of version https://github.com/microsoft/STL/issues/54) but there's currently not any | |
30 // pressing need for this for MSVC & writing the write compiler version check is annoying. | |
31 // Checking for clang version is problematic due to the way that XCode lies about __clang_major__. | |
32 // Instead we use __has_builtin as the feature check to check clang. | |
33 // Context: https://github.com/capnproto/capnproto/issues/1305 | |
34 #ifdef __has_builtin | |
35 #if __has_builtin(__builtin_COLUMN) | |
36 #define KJ_CALLER_COLUMN() __builtin_COLUMN() | |
37 #else | |
38 #define KJ_CALLER_COLUMN() 0 | |
39 #endif | |
40 #else | |
41 #define KJ_CALLER_COLUMN() 0 | |
42 #endif | |
43 | |
44 #if KJ_CPP_STD > 201703L | |
45 #define KJ_COMPILER_SUPPORTS_SOURCE_LOCATION 1 | |
46 #elif defined(__has_builtin) | |
47 // Clang 9 added these builtins: https://releases.llvm.org/9.0.0/tools/clang/docs/LanguageExtensions.html | |
48 // Use __has_builtin as the way to detect this because __clang_major__ is unreliable (see above | |
49 // about issue with Xcode-provided clang). | |
50 #define KJ_COMPILER_SUPPORTS_SOURCE_LOCATION ( \ | |
51 __has_builtin(__builtin_FILE) && \ | |
52 __has_builtin(__builtin_LINE) && \ | |
53 __has_builtin(__builtin_FUNCTION) \ | |
54 ) | |
55 #elif __GNUC__ >= 5 | |
56 // GCC 5 supports the required builtins: https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Other-Builtins.html | |
57 #define KJ_COMPILER_SUPPORTS_SOURCE_LOCATION 1 | |
58 #endif | |
59 | |
60 namespace kj { | |
61 class SourceLocation { | |
62 // libc++ doesn't seem to implement <source_location> (or even <experimental/source_location>), so | |
63 // this is a non-STL wrapper over the compiler primitives (these are the same across MSVC/clang/ | |
64 // gcc). Additionally this uses kj::StringPtr for holding the strings instead of const char* which | |
65 // makes it integrate a little more nicely into KJ. | |
66 | |
67 struct Badge { explicit constexpr Badge() = default; }; | |
68 // Neat little trick to make sure we can never call SourceLocation with explicit arguments. | |
69 public: | |
70 #if !KJ_COMPILER_SUPPORTS_SOURCE_LOCATION | |
71 constexpr SourceLocation() : fileName("??"), function("??"), lineNumber(0), columnNumber(0) {} | |
72 // Constructs a dummy source location that's not pointing at anything. | |
73 #else | |
74 constexpr SourceLocation(Badge = Badge{}, const char* file = __builtin_FILE(), | |
75 const char* func = __builtin_FUNCTION(), uint line = __builtin_LINE(), | |
76 uint column = KJ_CALLER_COLUMN()) | |
77 : fileName(file), function(func), lineNumber(line), columnNumber(column) | |
78 {} | |
79 #endif | |
80 | |
81 #if KJ_COMPILER_SUPPORTS_SOURCE_LOCATION | |
82 // This can only be exposed if we actually generate valid SourceLocation objects as otherwise all | |
83 // SourceLocation objects would confusingly (and likely problematically) be equated equal. | |
84 constexpr bool operator==(const SourceLocation& o) const { | |
85 // Pointer equality is fine here based on how SourceLocation operates & how compilers will | |
86 // intern all duplicate string constants. | |
87 return fileName == o.fileName && function == o.function && lineNumber == o.lineNumber && | |
88 columnNumber == o.columnNumber; | |
89 } | |
90 #endif | |
91 | |
92 const char* fileName; | |
93 const char* function; | |
94 uint lineNumber; | |
95 uint columnNumber; | |
96 }; | |
97 | |
98 kj::String KJ_STRINGIFY(const SourceLocation& l); | |
99 | |
100 class NoopSourceLocation { | |
101 // This is used in places where we want to conditionally compile out tracking the source location. | |
102 // As such it intentionally lacks all the features but the default constructor so that the API | |
103 // isn't accidentally used in the wrong compilation context. | |
104 }; | |
105 | |
106 KJ_UNUSED static kj::String KJ_STRINGIFY(const NoopSourceLocation& l) { | |
107 return kj::String(); | |
108 } | |
109 } // namespace kj | |
110 | |
111 KJ_END_HEADER |