Skip to content

Commit b943650

Browse files
committed
added constness_ptr as pointer wrapper to ensure actual method constness [skip ci]
1 parent cb6083f commit b943650

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

lib/constnessptr.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* -*- C++ -*-
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2025 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+
//---------------------------------------------------------------------------
20+
#ifndef constnessPtrH
21+
#define constnessPtrH
22+
//---------------------------------------------------------------------------
23+
24+
#include "config.h"
25+
26+
// as std::optional behaves similarly so we could use that instead of if we ever move to C++17.
27+
// it is not a simple drop-in as our "operator bool()" indicates if the pointer is non-null
28+
// whereas std::optional indicates if a value is set.
29+
//
30+
// This is similar to std::experimental::propagate_const
31+
// see https://en.cppreference.com/w/cpp/experimental/propagate_const
32+
template<typename T>
33+
class constness_ptr
34+
{
35+
public:
36+
explicit constness_ptr(T* p)
37+
: mPtr(p)
38+
{}
39+
40+
T* get() NOEXCEPT {
41+
return mPtr;
42+
}
43+
44+
const T* get() const NOEXCEPT {
45+
return mPtr;
46+
}
47+
48+
T* operator->() NOEXCEPT {
49+
return mPtr;
50+
}
51+
52+
const T* operator->() const NOEXCEPT {
53+
return mPtr;
54+
}
55+
56+
T& operator*() NOEXCEPT {
57+
return *mPtr;
58+
}
59+
60+
const T& operator*() const NOEXCEPT {
61+
return *mPtr;
62+
}
63+
64+
explicit operator bool() const NOEXCEPT {
65+
return mPtr != nullptr;
66+
}
67+
68+
private:
69+
T* mPtr;
70+
};
71+
72+
#endif // constnessPtrH

lib/cppcheck.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<ClInclude Include="clangimport.h" />
134134
<ClInclude Include="color.h" />
135135
<ClInclude Include="config.h" />
136+
<ClInclude Include="constnessptr.h" />
136137
<ClInclude Include="cppcheck.h" />
137138
<ClInclude Include="ctu.h" />
138139
<ClInclude Include="errorlogger.h" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "constnessptr.h"
2+
#include "utils.h"
3+
4+
struct S
5+
{
6+
void f();
7+
void f_c() const;
8+
};
9+
10+
struct S1
11+
{
12+
explicit S1(S*s) : mS(s) {}
13+
constness_ptr<S> mS;
14+
};
15+
16+
void f()
17+
{
18+
S s;
19+
S1 s1(&s);
20+
s1.mS->f();
21+
s1.mS->f_c();
22+
utils::as_const(s1).mS->f_c();
23+
#ifdef BAD
24+
utils::as_const(s1).mS->f();
25+
#endif
26+
}

test/constness_ptr/run.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/
4+
LIBDIR="$DIR"../../lib
5+
6+
ec=0
7+
gcc -c constness_ptr_test.cpp -Wall -Wextra -I$LIBDIR || ec=1
8+
gcc -c constness_ptr_test.cpp -Wall -Wextra -I$LIBDIR -DBAD && ec=1
9+
exit $ec

tools/dmake/dmake.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ int main(int argc, char **argv)
468468
libfiles_h.emplace("analyzer.h");
469469
libfiles_h.emplace("calculate.h");
470470
libfiles_h.emplace("config.h");
471+
libfiles_h.emplace("constnessptr.h");
471472
libfiles_h.emplace("filesettings.h");
472473
libfiles_h.emplace("findtoken.h");
473474
libfiles_h.emplace("json.h");

0 commit comments

Comments
 (0)