FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

added `constness_ptr` as pointer wrapper to ensure actual method constness by firewave · Pull Request #4785 · cppcheck-opensource/cppcheck · GitHub

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (2) .h  (1) .sh  (1) .vcxproj  (1) All 4 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
72 changes: 72 additions & 0 deletions lib/constnessptr.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* -*- C++ -*-
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

//---------------------------------------------------------------------------
#ifndef constnessPtrH
#define constnessPtrH
//---------------------------------------------------------------------------

#include "config.h"

// as std::optional behaves similarly so we could use that instead of if we ever move to C++17.
// it is not a simple drop-in as our "operator bool()" indicates if the pointer is non-null
// whereas std::optional indicates if a value is set.
//
// This is similar to std::experimental::propagate_const
// see https://en.cppreference.com/w/cpp/experimental/propagate_const
template<typename T>
class constness_ptr
{
public:
explicit constness_ptr(T* p)
: mPtr(p)
{}

T* get() NOEXCEPT {
return mPtr;
}

const T* get() const NOEXCEPT {
return mPtr;
}

T* operator->() NOEXCEPT {
return mPtr;
}

const T* operator->() const NOEXCEPT {
return mPtr;
}

T& operator*() NOEXCEPT {
return *mPtr;
}

const T& operator*() const NOEXCEPT {
return *mPtr;
}

explicit operator bool() const NOEXCEPT {
return mPtr != nullptr;
}

private:
T* mPtr;
};

#endif // constnessPtrH
1 change: 1 addition & 0 deletions lib/cppcheck.vcxproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<ClInclude Include="clangimport.h" />
<ClInclude Include="color.h" />
<ClInclude Include="config.h" />
<ClInclude Include="constnessptr.h" />
<ClInclude Include="cppcheck.h" />
<ClInclude Include="ctu.h" />
<ClInclude Include="errorlogger.h" />
Expand Down
26 changes: 26 additions & 0 deletions test/constness_ptr/constness_ptr_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "constnessptr.h"
#include "utils.h"

struct S
{
void f();
void f_c() const;
};

struct S1
{
explicit S1(S*s) : mS(s) {}
constness_ptr<S> mS;
};

void f()
{
S s;
S1 s1(&s);
s1.mS->f();
s1.mS->f_c();
utils::as_const(s1).mS->f_c();
#ifdef BAD
utils::as_const(s1).mS->f();
#endif
}
9 changes: 9 additions & 0 deletions test/constness_ptr/run.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/
LIBDIR="$DIR"../../lib

ec=0
gcc -c constness_ptr_test.cpp -Wall -Wextra -I$LIBDIR || ec=1
gcc -c constness_ptr_test.cpp -Wall -Wextra -I$LIBDIR -DBAD && ec=1
exit $ec
1 change: 1 addition & 0 deletions tools/dmake/dmake.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ int main(int argc, char **argv)
libfiles_h.emplace("analyzer.h");
libfiles_h.emplace("calculate.h");
libfiles_h.emplace("config.h");
libfiles_h.emplace("constnessptr.h");
libfiles_h.emplace("filesettings.h");
libfiles_h.emplace("findtoken.h");
libfiles_h.emplace("json.h");
Expand Down

Back | FazBrowse Home | New Git URL