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