Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit f322cbf

Browse files
authored
Adding Static Analysis and other checks to clang-tidy (#810)
* Stopped disabling default checks 'clang-diagnostic-*,clang-analyzer-*'. * Added cert* check to clang-tidy * Fixed the clang-format version for Linux. * Fixed Env:/AGENT_OS
1 parent d020bab commit f322cbf

File tree

18 files changed

+101
-69
lines changed

18 files changed

+101
-69
lines changed

bootstrap.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Push-Location (Join-Path $PSScriptRoot "./src/Simulation/qdk_sim_rs")
1919
--version $Env:NUGET_VERSION;
2020
Pop-Location
2121

22-
if (-not (Test-Path Env:AGENT_OS)) { # If not CI build, i.e. local build (if AGENT_OS envvar is not defined)
22+
if (-not (Test-Path Env:/AGENT_OS)) { # If not CI build, i.e. local build (if AGENT_OS envvar is not defined)
2323
if ($Env:ENABLE_NATIVE -ne "false") {
2424
Write-Host "Build release flavor of the native simulator"
2525
$Env:BUILD_CONFIGURATION = "Release"

src/Qir/.clang-tidy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Checks:
2-
'-*,bugprone-*,-readability-*,readability-identifier-*,readability-braces-around-statements'
2+
'bugprone-*,readability-identifier-*,readability-braces-around-statements,cert*,\
3+
-llvmlibc-callee-namespace,-llvmlibc-implementation-in-namespace,\
4+
-llvmlibc-restrict-system-libc-headers,-modernize-use-trailing-return-type,\
5+
-fuchsia-default-arguments-calls,-fuchsia-default-arguments-declarations,
6+
-google-readability-casting'
7+
# TODO(rokuzmin): '*, . . .'
8+
39
WarningsAsErrors: '*'
410
HeaderFilterRegex: '.*'
511

src/Qir/Runtime/lib/QIR/.clang-tidy

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Qir/Runtime/lib/QIR/QubitManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ namespace Quantum
181181
sharedQubitStatusArray = new QubitIdType[(size_t)qubitCapacity];
182182

183183
// These objects are passed by value (copies are created)
184-
QubitListInSharedArray FreeQubitsFresh(0, qubitCapacity - 1, sharedQubitStatusArray);
185-
RestrictedReuseArea outermostArea(FreeQubitsFresh);
184+
QubitListInSharedArray freeQubitsFresh(0, qubitCapacity - 1, sharedQubitStatusArray);
185+
RestrictedReuseArea outermostArea(freeQubitsFresh);
186186
freeQubitsInAreas.PushToBack(outermostArea);
187187

188188
freeQubitCount = qubitCapacity;
@@ -431,7 +431,7 @@ namespace Quantum
431431
// existing values (NonMarker or indexes in the array).
432432

433433
// Prepare new shared status array
434-
QubitIdType* newStatusArray = new QubitIdType[(size_t)requestedCapacity];
434+
auto* newStatusArray = new QubitIdType[(size_t)requestedCapacity];
435435
memcpy(newStatusArray, sharedQubitStatusArray, (size_t)qubitCapacity * sizeof(newStatusArray[0]));
436436
QubitListInSharedArray newFreeQubits(qubitCapacity, requestedCapacity - 1, newStatusArray);
437437

src/Qir/Runtime/lib/QIR/arrays.cpp

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ int QirArray::Release()
5454
return rc;
5555
}
5656

57-
QirArray::QirArray(TItemCount qubits_count)
58-
: count(qubits_count)
57+
QirArray::QirArray(TItemCount qubitsCount)
58+
: count(qubitsCount)
5959
, itemSizeInBytes((TItemSize)sizeof(void*))
6060
, ownsQubits(true)
6161
, refCount(1)
@@ -81,25 +81,25 @@ QirArray::QirArray(TItemCount qubits_count)
8181
}
8282
}
8383

84-
QirArray::QirArray(TItemCount count_items, TItemSize item_size_bytes, TDimCount dimCount, TDimContainer&& dimSizes)
85-
: count(count_items)
84+
QirArray::QirArray(TItemCount countItems, TItemSize itemSizeBytes, TDimCount dimCount, TDimContainer&& dimSizes)
85+
: count(countItems)
8686

8787
// Each array item needs to be properly aligned. Let's align them by correcting the `itemSizeInBytes`.
8888
, itemSizeInBytes(
89-
((item_size_bytes == 1) || (item_size_bytes == 2) || (item_size_bytes == 4) ||
90-
((item_size_bytes % sizeof(size_t)) == 0) // For built-in types or multiples of architecture alignment
89+
((itemSizeBytes == 1) || (itemSizeBytes == 2) || (itemSizeBytes == 4) ||
90+
((itemSizeBytes % sizeof(size_t)) == 0) // For built-in types or multiples of architecture alignment
9191
)
92-
? item_size_bytes // leave their natural alignment.
93-
// Other types align on the architecture boundary `sizeof(size_t)`:
94-
// 4 bytes on 32-bit arch, 8 on 64-bit arch.
95-
: item_size_bytes + sizeof(size_t) - (item_size_bytes % sizeof(size_t)))
92+
? itemSizeBytes // leave their natural alignment.
93+
// Other types align on the architecture boundary `sizeof(size_t)`:
94+
// 4 bytes on 32-bit arch, 8 on 64-bit arch.
95+
: itemSizeBytes + sizeof(size_t) - (itemSizeBytes % sizeof(size_t)))
9696

9797
, dimensions(dimCount)
9898
, dimensionSizes(std::move(dimSizes))
9999
, ownsQubits(false)
100100
, refCount(1)
101101
{
102-
assert(item_size_bytes != 0);
102+
assert(itemSizeBytes != 0);
103103
assert(dimCount > 0);
104104

105105
if (GlobalContext() != nullptr)
@@ -112,18 +112,18 @@ QirArray::QirArray(TItemCount count_items, TItemSize item_size_bytes, TDimCount
112112
assert(this->dimensionSizes.empty() || this->dimensionSizes[0] == this->count);
113113
if (this->dimensionSizes.empty())
114114
{
115-
this->dimensionSizes.push_back(count_items);
115+
this->dimensionSizes.push_back(countItems);
116116
}
117117
}
118118

119119
assert(this->count * (TBufSize)itemSizeInBytes < std::numeric_limits<TBufSize>::max());
120120
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
121-
const TBufSize buffer_size = this->count * itemSizeInBytes;
122-
if (buffer_size > 0)
121+
const TBufSize bufferSize = this->count * itemSizeInBytes;
122+
if (bufferSize > 0)
123123
{
124-
this->buffer = new char[buffer_size];
125-
assert(buffer_size <= std::numeric_limits<size_t>::max());
126-
memset(this->buffer, 0, (size_t)buffer_size);
124+
this->buffer = new char[bufferSize];
125+
assert(bufferSize <= std::numeric_limits<size_t>::max());
126+
memset(this->buffer, 0, (size_t)bufferSize);
127127
}
128128
else
129129
{
@@ -178,26 +178,26 @@ void QirArray::Append(const QirArray* other)
178178

179179
assert((TBufSize)(other->count) * other->itemSizeInBytes < std::numeric_limits<TBufSize>::max());
180180
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
181-
const TBufSize other_size = other->count * other->itemSizeInBytes;
181+
const TBufSize otherSize = other->count * other->itemSizeInBytes;
182182

183-
if (other_size == 0)
183+
if (otherSize == 0)
184184
{
185185
return;
186186
}
187187

188188
assert((TBufSize)(this->count) * this->itemSizeInBytes < std::numeric_limits<TBufSize>::max());
189189
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
190-
const TBufSize this_size = this->count * this->itemSizeInBytes;
190+
const TBufSize thisSize = this->count * this->itemSizeInBytes;
191191

192-
char* new_buffer = new char[this_size + other_size];
193-
if (this_size)
192+
char* newBuffer = new char[thisSize + otherSize];
193+
if (thisSize)
194194
{
195-
memcpy(new_buffer, this->buffer, this_size);
195+
memcpy(newBuffer, this->buffer, thisSize);
196196
}
197-
memcpy(&new_buffer[this_size], other->buffer, other_size);
197+
memcpy(&newBuffer[thisSize], other->buffer, otherSize);
198198

199199
delete[] this->buffer;
200-
this->buffer = new_buffer;
200+
this->buffer = newBuffer;
201201
this->count += other->count;
202202
this->dimensionSizes[0] = this->count;
203203
}
@@ -279,11 +279,10 @@ extern "C"
279279
__quantum__rt__qubit_release_array(qa);
280280
}
281281

282-
// TODO: Use `QirArray::TItemSize itemSizeInBytes, QirArray::TItemCount count_items` (breaking change):
283-
QirArray* __quantum__rt__array_create_1d(int32_t itemSizeInBytes, int64_t count_items)
282+
QirArray* __quantum__rt__array_create_1d(int32_t itemSizeInBytes, int64_t countItems)
284283
{
285284
assert(itemSizeInBytes > 0);
286-
return new QirArray((QirArray::TItemCount)count_items, (QirArray::TItemSize)itemSizeInBytes);
285+
return new QirArray((QirArray::TItemCount)countItems, (QirArray::TItemSize)itemSizeInBytes);
287286
}
288287

289288
// Bucketing of addref/release is non-standard so for now we'll keep the more traditional addref/release semantics
@@ -560,6 +559,10 @@ extern "C"
560559
const QirArray::TItemCount sliceItemsCount = std::accumulate(
561560
sliceDims.begin(), sliceDims.end(), (QirArray::TItemCount)1, std::multiplies<QirArray::TItemCount>());
562561
QirArray* slice = new QirArray(sliceItemsCount, itemSizeInBytes, dimensions, std::move(sliceDims));
562+
if (nullptr == slice->buffer)
563+
{
564+
return slice;
565+
}
563566
const QirArray::TItemCount singleIndexRunCount = RunCount(array->dimensionSizes, (QirArray::TDimCount)dim);
564567
const QirArray::TItemCount rowCount = singleIndexRunCount * array->dimensionSizes[(size_t)dim];
565568

@@ -636,13 +639,18 @@ extern "C"
636639
const QirArray::TItemCount projectItemsCount = std::accumulate(
637640
projectDims.begin(), projectDims.end(), (QirArray::TItemCount)1, std::multiplies<QirArray::TItemCount>());
638641
QirArray* project = new QirArray(projectItemsCount, itemSizeInBytes, dimensions - 1, std::move(projectDims));
642+
if (nullptr == project->buffer)
643+
{
644+
return project;
645+
}
639646

640647
const QirArray::TItemCount singleIndexRunCount = RunCount(array->dimensionSizes, (QirArray::TDimCount)dim);
641648
const QirArray::TItemCount rowCount = singleIndexRunCount * array->dimensionSizes[(size_t)dim];
642649

643650
assert((QirArray::TBufSize)singleIndexRunCount * itemSizeInBytes <
644651
std::numeric_limits<QirArray::TBufSize>::max());
645652
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
653+
646654
const QirArray::TBufSize chunkSize = singleIndexRunCount * itemSizeInBytes;
647655

648656
QirArray::TItemCount dst = 0;

src/Qir/Runtime/lib/QIR/callables.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ extern "C"
102102
{
103103
for (int i = increment; i < 0; i++)
104104
{
105-
(void)callable->Release();
105+
if (0 == callable->Release())
106+
{
107+
assert(-1 == i && "Attempting to decrement reference count below zero!");
108+
break;
109+
}
106110
}
107111
}
108112
}

src/Qir/Runtime/lib/QIR/strings.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static QirString* CreateOrReuseAlreadyAllocated(std::string&& str)
2828
if (alreadyAllocated != AllocatedStrings().end())
2929
{
3030
qstr = alreadyAllocated->second;
31-
assert(qstr->str.compare(str) == 0);
31+
assert(qstr->str == str);
3232
qstr->refCount++;
3333
}
3434
else
@@ -95,7 +95,7 @@ extern "C"
9595
// Returns true if the two strings are equal, false otherwise.
9696
bool __quantum__rt__string_equal(QirString* left, QirString* right) // NOLINT
9797
{
98-
assert((left == right) == (left->str.compare(right->str) == 0));
98+
assert((left == right) == (left->str == right->str));
9999
return left == right;
100100
}
101101

@@ -115,7 +115,7 @@ extern "C"
115115

116116
// Remove padding zeros from the decimal part (relies on the fact that the output for integers always contains
117117
// period).
118-
std::size_t pos1 = str.find_last_not_of("0");
118+
std::size_t pos1 = str.find_last_not_of('0');
119119
if (pos1 != std::string::npos)
120120
{
121121
str.erase(pos1 + 1);

src/Qir/Runtime/lib/QSharpCore/.clang-tidy

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Qir/Runtime/prerequisites.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#Requires -Version 6.0
55

66
if ($Env:ENABLE_QIRRUNTIME -ne "false") {
7-
if (($IsWindows) -or ((Test-Path Env:AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Win")))) {
7+
if (($IsWindows) -or ((Test-Path Env:/AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Win")))) {
88
if (!(Get-Command clang -ErrorAction SilentlyContinue) -or `
99
!(Get-Command clang-format -ErrorAction SilentlyContinue)) {
1010
choco install llvm --version=11.1.0
@@ -29,10 +29,10 @@ if ($Env:ENABLE_QIRRUNTIME -ne "false") {
2929
} else {
3030
if (Get-Command sudo -ErrorAction SilentlyContinue) {
3131
sudo apt update
32-
sudo apt-get install -y ninja-build clang-11 clang-tidy-11
32+
sudo apt-get install -y ninja-build clang-11 clang-tidy-11 clang-format-11
3333
} else {
3434
apt update
35-
apt-get install -y ninja-build clang-11 clang-tidy-11
35+
apt-get install -y ninja-build clang-11 clang-tidy-11 clang-format-11
3636
}
3737
}
3838
}

src/Qir/Runtime/public/CoreTypes.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class QUBIT;
3636
typedef QUBIT* Qubit; // Not a pointer to a memory location, just an integer - qubit id.
3737

3838
class RESULT;
39-
typedef RESULT* Result; // TODO: Replace with `typedef uintXX_t Result`, where XX is 8|16|32|64.
39+
typedef RESULT* Result; // TODO(rokuzmin): Replace with `typedef uintXX_t Result`, where XX is 8|16|32|64.
4040
// Remove all the `RESULT`.
4141

4242
enum ResultValue

0 commit comments

Comments
 (0)