/src/capnproto/c++/src/kj/source-location.h
Line | Count | Source (jump to first uncovered line) |
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 | | #define KJ_COMPILER_SUPPORTS_SOURCE_LOCATION 1 |
45 | | |
46 | | namespace kj { |
47 | | class SourceLocation { |
48 | | // libc++ doesn't seem to implement <source_location> (or even <experimental/source_location>), so |
49 | | // this is a non-STL wrapper over the compiler primitives (these are the same across MSVC/clang/ |
50 | | // gcc). Additionally this uses kj::StringPtr for holding the strings instead of const char* which |
51 | | // makes it integrate a little more nicely into KJ. |
52 | | |
53 | | struct Badge { explicit constexpr Badge() = default; }; |
54 | | // Neat little trick to make sure we can never call SourceLocation with explicit arguments. |
55 | | public: |
56 | | #if !KJ_COMPILER_SUPPORTS_SOURCE_LOCATION |
57 | | constexpr SourceLocation() : fileName("??"), function("??"), lineNumber(0), columnNumber(0) {} |
58 | | // Constructs a dummy source location that's not pointing at anything. |
59 | | #else |
60 | | constexpr SourceLocation(Badge = Badge{}, const char* file = __builtin_FILE(), |
61 | | const char* func = __builtin_FUNCTION(), uint line = __builtin_LINE(), |
62 | | uint column = KJ_CALLER_COLUMN()) |
63 | | : fileName(file), function(func), lineNumber(line), columnNumber(column) |
64 | 0 | {} |
65 | | #endif |
66 | | |
67 | | #if KJ_COMPILER_SUPPORTS_SOURCE_LOCATION |
68 | | // This can only be exposed if we actually generate valid SourceLocation objects as otherwise all |
69 | | // SourceLocation objects would confusingly (and likely problematically) be equated equal. |
70 | 0 | constexpr bool operator==(const SourceLocation& o) const { |
71 | 0 | // Pointer equality is fine here based on how SourceLocation operates & how compilers will |
72 | 0 | // intern all duplicate string constants. |
73 | 0 | return fileName == o.fileName && function == o.function && lineNumber == o.lineNumber && |
74 | 0 | columnNumber == o.columnNumber; |
75 | 0 | } |
76 | | #endif |
77 | | |
78 | | const char* fileName; |
79 | | const char* function; |
80 | | uint lineNumber; |
81 | | uint columnNumber; |
82 | | }; |
83 | | |
84 | | kj::String KJ_STRINGIFY(const SourceLocation& l); |
85 | | |
86 | | class NoopSourceLocation { |
87 | | // This is used in places where we want to conditionally compile out tracking the source location. |
88 | | // As such it intentionally lacks all the features but the default constructor so that the API |
89 | | // isn't accidentally used in the wrong compilation context. |
90 | | }; |
91 | | |
92 | 0 | KJ_UNUSED static kj::String KJ_STRINGIFY(const NoopSourceLocation& l) { |
93 | 0 | return kj::String(); |
94 | 0 | } Unexecuted instantiation: llvm-fuzzer-testcase.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: test-util.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: test.capnp.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: capability.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: layout.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: any.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: message.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: serialize.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: schema.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: dynamic.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: stringify.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: arena.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: async.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: source-location.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) Unexecuted instantiation: mutex.c++:kj::operator*(kj::_::Stringifier, kj::NoopSourceLocation const&) |
95 | | } // namespace kj |
96 | | |
97 | | KJ_END_HEADER |