/src/cppcheck/lib/sourcelocation.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- C++ -*- |
2 | | * Cppcheck - A tool for static C/C++ code analysis |
3 | | * Copyright (C) 2007-2024 Cppcheck team. |
4 | | * |
5 | | * This program is free software: you can redistribute it and/or modify |
6 | | * it under the terms of the GNU General Public License as published by |
7 | | * the Free Software Foundation, either version 3 of the License, or |
8 | | * (at your option) any later version. |
9 | | * |
10 | | * This program is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | * GNU General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU General Public License |
16 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | */ |
18 | | |
19 | | #ifndef sourcelocationH |
20 | | #define sourcelocationH |
21 | | |
22 | | #include "config.h" |
23 | | |
24 | | #ifdef __CPPCHECK__ |
25 | | #define CPPCHECK_HAS_SOURCE_LOCATION 0 |
26 | | #define CPPCHECK_HAS_SOURCE_LOCATION_TS 0 |
27 | | #define CPPCHECK_HAS_SOURCE_LOCATION_INTRINSICS 0 |
28 | | #else |
29 | | |
30 | | #if __has_include(<source_location>) && __cplusplus >= 202003L |
31 | | #define CPPCHECK_HAS_SOURCE_LOCATION 1 |
32 | | #else |
33 | | #define CPPCHECK_HAS_SOURCE_LOCATION 0 |
34 | | #endif |
35 | | #if __has_include(<experimental/source_location>) && __cplusplus >= 201402L |
36 | | #define CPPCHECK_HAS_SOURCE_LOCATION_TS 1 |
37 | | #else |
38 | | #define CPPCHECK_HAS_SOURCE_LOCATION_TS 0 |
39 | | #endif |
40 | | |
41 | | #if __has_builtin(__builtin_FILE) |
42 | | #define CPPCHECK_HAS_SOURCE_LOCATION_INTRINSICS 1 |
43 | | #if !__has_builtin(__builtin_COLUMN) |
44 | | #define __builtin_COLUMN() 0 |
45 | | #endif |
46 | | #else |
47 | | #define CPPCHECK_HAS_SOURCE_LOCATION_INTRINSICS 0 |
48 | | #endif |
49 | | |
50 | | #endif |
51 | | |
52 | | #if CPPCHECK_HAS_SOURCE_LOCATION |
53 | | #include <source_location> |
54 | | using SourceLocation = std::source_location; |
55 | | #elif CPPCHECK_HAS_SOURCE_LOCATION_TS |
56 | | #include <experimental/source_location> |
57 | | using SourceLocation = std::experimental::source_location; |
58 | | #else |
59 | | #include <cstdint> |
60 | | struct SourceLocation { |
61 | | #if CPPCHECK_HAS_SOURCE_LOCATION_INTRINSICS |
62 | | static SourceLocation current(std::uint_least32_t line = __builtin_LINE(), |
63 | | std::uint_least32_t column = __builtin_COLUMN(), |
64 | | const char* file_name = __builtin_FILE(), |
65 | | const char* function_name = __builtin_FUNCTION()) |
66 | 224k | { |
67 | 224k | SourceLocation result{}; |
68 | 224k | result.m_line = line; |
69 | 224k | result.m_column = column; |
70 | 224k | result.m_file_name = file_name; |
71 | 224k | result.m_function_name = function_name; |
72 | 224k | return result; |
73 | 224k | } |
74 | | #else |
75 | | static SourceLocation current() { |
76 | | return SourceLocation(); |
77 | | } |
78 | | #endif |
79 | | std::uint_least32_t m_line = 0; |
80 | | std::uint_least32_t m_column = 0; |
81 | | const char* m_file_name = ""; |
82 | | const char* m_function_name = ""; |
83 | 0 | std::uint_least32_t line() const { |
84 | 0 | return m_line; |
85 | 0 | } |
86 | 0 | std::uint_least32_t column() const { |
87 | 0 | return m_column; |
88 | 0 | } |
89 | 0 | const char* file_name() const { |
90 | 0 | return m_file_name; |
91 | 0 | } |
92 | 0 | const char* function_name() const { |
93 | 0 | return m_function_name; |
94 | 0 | } |
95 | | }; |
96 | | #endif |
97 | | |
98 | | #endif |