Commit 4c7f64eb authored by Hans Johnson's avatar Hans Johnson
Browse files

COMP: Prefer const member functions

Finds non-static member functions that can be made const because the functions
don’t use this in a non-const way.

This check tries to annotate methods according to logical constness (not
physical constness). Therefore, it will suggest to add a const qualifier to a
non-const method only if this method does something that is already possible
though the public interface on a const pointer to the object:

reading a public member variable calling a public const-qualified member
function returning const-qualified this passing const-qualified this as a
parameter.  This check will also suggest to add a const qualifier to a
non-const method if this method uses private data and functions in a limited
number of ways where logical constness and physical constness coincide:
 - reading a member variable of builtin type
Specifically, this check will not suggest to add a const to a non-const
method if the method reads a private member variable of pointer type because
that allows to modify the pointee which might not preserve logical constness.
For the same reason, it does not allow to call private member functions or
member functions on private member variables.

In addition, this check ignores functions that
 - are declared virtual
 - contain a const_cast
 - are templated or part of a class template
 - have an empty body
 - do not (implicitly) use this at all (see readability-convert-member-functions-to-static).
parent acc916ed
......@@ -316,21 +316,21 @@ public:
SystemInformationImplementation();
~SystemInformationImplementation();
const char* GetVendorString();
const char* GetVendorString() const;
const char* GetVendorID();
std::string GetTypeID();
std::string GetFamilyID();
std::string GetModelID();
std::string GetModelName();
std::string GetSteppingCode();
const char* GetExtendedProcessorName();
const char* GetProcessorSerialNumber();
int GetProcessorCacheSize();
unsigned int GetLogicalProcessorsPerPhysical();
float GetProcessorClockFrequency();
int GetProcessorAPICID();
int GetProcessorCacheXSize(long int);
bool DoesCPUSupportFeature(long int);
std::string GetTypeID() const;
std::string GetFamilyID() const;
std::string GetModelID() const;
std::string GetModelName() const;
std::string GetSteppingCode() const;
const char* GetExtendedProcessorName() const;
const char* GetProcessorSerialNumber() const;
int GetProcessorCacheSize() const;
unsigned int GetLogicalProcessorsPerPhysical() const;
float GetProcessorClockFrequency() const;
int GetProcessorAPICID() const;
int GetProcessorCacheXSize(long int) const;
bool DoesCPUSupportFeature(long int) const;
const char* GetOSName();
const char* GetHostname();
......@@ -339,18 +339,18 @@ public:
const char* GetOSVersion();
const char* GetOSPlatform();
bool Is64Bits();
bool Is64Bits() const;
unsigned int GetNumberOfLogicalCPU(); // per physical cpu
unsigned int GetNumberOfPhysicalCPU();
unsigned int GetNumberOfLogicalCPU() const; // per physical cpu
unsigned int GetNumberOfPhysicalCPU() const;
bool DoesCPUSupportCPUID();
// Retrieve memory information in MiB.
size_t GetTotalVirtualMemory();
size_t GetAvailableVirtualMemory();
size_t GetTotalPhysicalMemory();
size_t GetAvailablePhysicalMemory();
size_t GetTotalVirtualMemory() const;
size_t GetAvailableVirtualMemory() const;
size_t GetTotalPhysicalMemory() const;
size_t GetAvailablePhysicalMemory() const;
LongLong GetProcessId();
......@@ -519,7 +519,7 @@ protected:
void CPUCountWindows(); // For windows
unsigned char GetAPICId(); // For windows
bool IsSMTSupported();
bool IsSMTSupported() const;
static LongLong GetCyclesDifference(DELAY_FUNC, unsigned int); // For windows
// For Linux and Cygwin, /proc/cpuinfo formats are slightly different
......@@ -1607,7 +1607,7 @@ void SystemInformationImplementation::RunMemoryCheck()
}
/** Get the vendor string */
const char* SystemInformationImplementation::GetVendorString()
const char* SystemInformationImplementation::GetVendorString() const
{
return this->ChipID.Vendor.c_str();
}
......@@ -1803,7 +1803,7 @@ const char* SystemInformationImplementation::GetVendorID()
}
/** Return the type ID of the CPU */
std::string SystemInformationImplementation::GetTypeID()
std::string SystemInformationImplementation::GetTypeID() const
{
std::ostringstream str;
str << this->ChipID.Type;
......@@ -1811,7 +1811,7 @@ std::string SystemInformationImplementation::GetTypeID()
}
/** Return the family of the CPU present */
std::string SystemInformationImplementation::GetFamilyID()
std::string SystemInformationImplementation::GetFamilyID() const
{
std::ostringstream str;
str << this->ChipID.Family;
......@@ -1819,7 +1819,7 @@ std::string SystemInformationImplementation::GetFamilyID()
}
// Return the model of CPU present */
std::string SystemInformationImplementation::GetModelID()
std::string SystemInformationImplementation::GetModelID() const
{
std::ostringstream str;
str << this->ChipID.Model;
......@@ -1827,13 +1827,13 @@ std::string SystemInformationImplementation::GetModelID()
}
// Return the model name of CPU present */
std::string SystemInformationImplementation::GetModelName()
std::string SystemInformationImplementation::GetModelName() const
{
return this->ChipID.ModelName;
}
/** Return the stepping code of the CPU present. */
std::string SystemInformationImplementation::GetSteppingCode()
std::string SystemInformationImplementation::GetSteppingCode() const
{
std::ostringstream str;
str << this->ChipID.Revision;
......@@ -1841,44 +1841,46 @@ std::string SystemInformationImplementation::GetSteppingCode()
}
/** Return the stepping code of the CPU present. */
const char* SystemInformationImplementation::GetExtendedProcessorName()
const char* SystemInformationImplementation::GetExtendedProcessorName() const
{
return this->ChipID.ProcessorName.c_str();
}
/** Return the serial number of the processor
* in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */
const char* SystemInformationImplementation::GetProcessorSerialNumber()
const char* SystemInformationImplementation::GetProcessorSerialNumber() const
{
return this->ChipID.SerialNumber.c_str();
}
/** Return the logical processors per physical */
unsigned int SystemInformationImplementation::GetLogicalProcessorsPerPhysical()
const
{
return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical;
}
/** Return the processor clock frequency. */
float SystemInformationImplementation::GetProcessorClockFrequency()
float SystemInformationImplementation::GetProcessorClockFrequency() const
{
return this->CPUSpeedInMHz;
}
/** Return the APIC ID. */
int SystemInformationImplementation::GetProcessorAPICID()
int SystemInformationImplementation::GetProcessorAPICID() const
{
return this->Features.ExtendedFeatures.APIC_ID;
}
/** Return the L1 cache size. */
int SystemInformationImplementation::GetProcessorCacheSize()
int SystemInformationImplementation::GetProcessorCacheSize() const
{
return this->Features.L1CacheSize;
}
/** Return the chosen cache size. */
int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
int SystemInformationImplementation::GetProcessorCacheXSize(
long int dwCacheID) const
{
switch (dwCacheID) {
case SystemInformation::CPU_FEATURE_L1CACHE:
......@@ -1891,7 +1893,8 @@ int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
return -1;
}
bool SystemInformationImplementation::DoesCPUSupportFeature(long int dwFeature)
bool SystemInformationImplementation::DoesCPUSupportFeature(
long int dwFeature) const
{
bool bHasFeature = false;
......@@ -4292,24 +4295,24 @@ bool SystemInformationImplementation::QueryMemory()
}
/** */
size_t SystemInformationImplementation::GetTotalVirtualMemory()
size_t SystemInformationImplementation::GetTotalVirtualMemory() const
{
return this->TotalVirtualMemory;
}
/** */
size_t SystemInformationImplementation::GetAvailableVirtualMemory()
size_t SystemInformationImplementation::GetAvailableVirtualMemory() const
{
return this->AvailableVirtualMemory;
}
size_t SystemInformationImplementation::GetTotalPhysicalMemory()
size_t SystemInformationImplementation::GetTotalPhysicalMemory() const
{
return this->TotalPhysicalMemory;
}
/** */
size_t SystemInformationImplementation::GetAvailablePhysicalMemory()
size_t SystemInformationImplementation::GetAvailablePhysicalMemory() const
{
return this->AvailablePhysicalMemory;
}
......@@ -4393,7 +4396,7 @@ void SystemInformationImplementation::DelayOverhead(unsigned int uiMS)
}
/** Works only for windows */
bool SystemInformationImplementation::IsSMTSupported()
bool SystemInformationImplementation::IsSMTSupported() const
{
return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical > 1;
}
......@@ -4475,13 +4478,13 @@ void SystemInformationImplementation::CPUCountWindows()
}
/** Return the number of logical CPUs on the system */
unsigned int SystemInformationImplementation::GetNumberOfLogicalCPU()
unsigned int SystemInformationImplementation::GetNumberOfLogicalCPU() const
{
return this->NumberOfLogicalCPU;
}
/** Return the number of physical CPUs on the system */
unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU()
unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU() const
{
return this->NumberOfPhysicalCPU;
}
......@@ -5502,7 +5505,7 @@ void SystemInformationImplementation::TrimNewline(std::string& output)
}
/** Return true if the machine is 64 bits */
bool SystemInformationImplementation::Is64Bits()
bool SystemInformationImplementation::Is64Bits() const
{
return this->OSIs64Bit;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment