capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

source-location.h (4830B)


      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 // GCC does not implement __builtin_COLUMN() as that's non-standard but MSVC & clang do.
     27 // MSVC does as of version https://github.com/microsoft/STL/issues/54) but there's currently not any
     28 // pressing need for this for MSVC & writing the write compiler version check is annoying.
     29 // Checking for clang version is problematic due to the way that XCode lies about __clang_major__.
     30 // Instead we use __has_builtin as the feature check to check clang.
     31 // Context: https://github.com/capnproto/capnproto/issues/1305
     32 #ifdef __has_builtin
     33 #if __has_builtin(__builtin_COLUMN)
     34 #define KJ_CALLER_COLUMN() __builtin_COLUMN()
     35 #else
     36 #define KJ_CALLER_COLUMN() 0
     37 #endif
     38 #else
     39 #define KJ_CALLER_COLUMN() 0
     40 #endif
     41 
     42 #if __cplusplus > 201703L
     43 #define KJ_COMPILER_SUPPORTS_SOURCE_LOCATION 1
     44 #elif defined(__has_builtin)
     45 // Clang 9 added these builtins: https://releases.llvm.org/9.0.0/tools/clang/docs/LanguageExtensions.html
     46 // Use __has_builtin as the way to detect this because __clang_major__ is unreliable (see above
     47 // about issue with Xcode-provided clang).
     48 #define KJ_COMPILER_SUPPORTS_SOURCE_LOCATION (   \
     49   __has_builtin(__builtin_FILE) &&               \
     50   __has_builtin(__builtin_LINE) &&               \
     51   __has_builtin(__builtin_FUNCTION)              \
     52   )
     53 #elif __GNUC__ >= 5
     54 // GCC 5 supports the required builtins: https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Other-Builtins.html
     55 #define KJ_COMPILER_SUPPORTS_SOURCE_LOCATION 1
     56 #endif
     57 
     58 namespace kj {
     59 class SourceLocation {
     60   // libc++ doesn't seem to implement <source_location> (or even <experimental/source_location>), so
     61   // this is a non-STL wrapper over the compiler primitives (these are the same across MSVC/clang/
     62   // gcc). Additionally this uses kj::StringPtr for holding the strings instead of const char* which
     63   // makes it integrate a little more nicely into KJ.
     64 
     65   struct Badge { explicit constexpr Badge() = default; };
     66   // Neat little trick to make sure we can never call SourceLocation with explicit arguments.
     67 public:
     68 #if !KJ_COMPILER_SUPPORTS_SOURCE_LOCATION
     69   constexpr SourceLocation() : fileName("??"), function("??"), lineNumber(0), columnNumber(0) {}
     70   // Constructs a dummy source location that's not pointing at anything.
     71 #else
     72   constexpr SourceLocation(Badge = Badge{}, const char* file = __builtin_FILE(),
     73       const char* func = __builtin_FUNCTION(), uint line = __builtin_LINE(),
     74       uint column = KJ_CALLER_COLUMN())
     75     : fileName(file), function(func), lineNumber(line), columnNumber(column)
     76   {}
     77 #endif
     78 
     79 #if KJ_COMPILER_SUPPORTS_SOURCE_LOCATION
     80   // This can only be exposed if we actually generate valid SourceLocation objects as otherwise all
     81   // SourceLocation objects would confusingly (and likely problematically) be equated equal.
     82   constexpr bool operator==(const SourceLocation& o) const {
     83     // Pointer equality is fine here based on how SourceLocation operates & how compilers will
     84     // intern all duplicate string constants.
     85     return fileName == o.fileName && function == o.function && lineNumber == o.lineNumber &&
     86         columnNumber == o.columnNumber;
     87   }
     88 #endif
     89 
     90   const char* fileName;
     91   const char* function;
     92   uint lineNumber;
     93   uint columnNumber;
     94 };
     95 
     96 kj::String KJ_STRINGIFY(const SourceLocation& l);
     97 
     98 class NoopSourceLocation {
     99   // This is used in places where we want to conditionally compile out tracking the source location.
    100   // As such it intentionally lacks all the features but the default constructor so that the API
    101   // isn't accidentally used in the wrong compilation context.
    102 };
    103 
    104 KJ_UNUSED static kj::String KJ_STRINGIFY(const NoopSourceLocation& l) {
    105   return kj::String();
    106 }
    107 }  // namespace kj