xref: /aosp_15_r20/external/federated-compute/fcp/base/source_location.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2019 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef FCP_BASE_SOURCE_LOCATION_H_
17 #define FCP_BASE_SOURCE_LOCATION_H_
18 
19 namespace fcp {
20 
21 #if (__clang_major__ >= 9) || (__GNUC__ >= 7)
22 // Using non-standard builtin extensions of gcc and clang to capture call-site
23 // location
24 #define FCP_HAS_SOURCE_LOCATION
25 #define FCP_BUILTIN_LINE() __builtin_LINE()
26 #define FCP_BUILTIN_FILE() __builtin_FILE()
27 #else
28 // If compiler feature unavailable replace with stub values
29 #define FCP_BUILTIN_LINE() (0)
30 #define FCP_BUILTIN_FILE() ("<unknown_source>")
31 
32 #endif
33 
34 class SourceLocationImpl {
35  public:
36   static constexpr SourceLocationImpl current(
37       // Builtins _must_ be referenced from default arguments, so they get
38       // evaluated at the callsite.
39       int line = FCP_BUILTIN_LINE(),
40       const char* file_name = FCP_BUILTIN_FILE()) {
41     return SourceLocationImpl(line, file_name);
42   }
line()43   constexpr int line() const { return line_; }
file_name()44   constexpr const char* file_name() const { return file_name_; }
45 
46  private:
SourceLocationImpl(int line,const char * file_name)47   constexpr SourceLocationImpl(int line, const char* file_name)
48       : line_(line), file_name_(file_name) {}
49   int line_;
50   const char* file_name_;
51 };
52 
53 using SourceLocation = SourceLocationImpl;
54 
55 }  // namespace fcp
56 
57 #endif  // FCP_BASE_SOURCE_LOCATION_H_
58