GCC Code Coverage Report


Directory: ./
File: lib/geogram/basic/assert.cpp
Date: 2026-09-07 02:36:43
Exec Total Coverage
Lines: 5 54 9.3%
Functions: 2 7 28.6%
Branches: 0 142 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #include <geogram/basic/assert.h>
41 #include <geogram/basic/logger.h>
42 #include <geogram/basic/process.h>
43 #include <stdlib.h>
44 #include <sstream>
45 #include <stdexcept>
46 #include <iostream>
47
48 #ifdef GEO_OS_WINDOWS
49 #include <intrin.h> // For __debugbreak()
50 #else
51 #include <sys/types.h>
52 #include <unistd.h>
53 #include <signal.h>
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #endif
57
58 namespace GEO {
59
60 namespace {
61 #ifdef GEO_DEBUG
62 AssertMode assert_mode_ = ASSERT_ABORT;
63 #else
64 AssertMode assert_mode_ = ASSERT_THROW;
65 #endif
66 bool aborting = false;
67 }
68
69 249 void set_assert_mode(AssertMode mode) {
70 249 assert_mode_ = mode;
71 249 }
72
73 105 AssertMode assert_mode() {
74 105 return assert_mode_;
75 }
76
77 void geo_abort() {
78 #ifdef GEO_OS_WINDOWS
79 std::cerr << "Aborting, press any key to continue" << std::endl;
80 std::getchar();
81 #endif
82 // Avoid assert in assert !!
83 if(aborting) {
84 Process::brute_force_kill();
85 }
86 aborting = true;
87 abort();
88 }
89
90 void geo_breakpoint() {
91 #ifdef GEO_COMPILER_MSVC
92 __debugbreak();
93 #else
94 geo_abort();
95 #endif
96 }
97
98 void geo_assertion_failed(
99 const std::string& condition_string,
100 const std::string& file, int line
101 ) {
102 std::ostringstream os;
103 os << "Assertion failed: " << condition_string << ".\n";
104 os << "File: " << file << ",\n";
105 os << "Line: " << line;
106
107 if(Logger::instance()->is_quiet()) {
108 std::cerr << os.str() << std::endl;
109 } else {
110 Logger::err("Assert") << os.str() << std::endl;
111 }
112 Process::print_stack_trace();
113
114 if(assert_mode_ == ASSERT_THROW) {
115 throw std::runtime_error(os.str());
116 } else if(assert_mode_ == ASSERT_ABORT) {
117 geo_abort();
118 } else {
119 geo_breakpoint();
120 }
121 }
122
123 void geo_range_assertion_failed(
124 double value, double min_value, double max_value,
125 const std::string& file, int line
126 ) {
127 std::ostringstream os;
128 os << "Range assertion failed: " << value
129 << " in [ " << min_value << " ... " << max_value << " ].\n";
130 os << "File: " << file << ",\n";
131 os << "Line: " << line;
132
133 if(assert_mode_ == ASSERT_THROW) {
134 if(Logger::instance()->is_quiet()) {
135 std::cerr << os.str()
136 << std::endl;
137 }
138 throw std::runtime_error(os.str());
139 } else {
140 Logger::err("Assert") << os.str() << std::endl;
141 geo_abort();
142 }
143 }
144
145 void geo_should_not_have_reached(
146 const std::string& file, int line
147 ) {
148 std::ostringstream os;
149 os << "Control should not have reached this point.\n";
150 os << "File: " << file << ",\n";
151 os << "Line: " << line;
152
153 if(assert_mode_ == ASSERT_THROW) {
154 if(Logger::instance()->is_quiet()) {
155 std::cerr << os.str()
156 << std::endl;
157 }
158 throw std::runtime_error(os.str());
159 } else {
160 Logger::err("Assert") << os.str() << std::endl;
161 geo_abort();
162 }
163 }
164 }
165