|
| 1 | +/** |
| 2 | + * Live Test C++ File for Tier 2 Native Fixer Validation |
| 3 | + * |
| 4 | + * This file contains INTENTIONAL issues that should be fixed by various tiers: |
| 5 | + * |
| 6 | + * Tier 1 (clang-format): |
| 7 | + * - Inconsistent indentation |
| 8 | + * - Missing/extra whitespace |
| 9 | + * - Brace placement issues |
| 10 | + * - Operator spacing |
| 11 | + * - Line length issues |
| 12 | + * |
| 13 | + * Tier 2 (clang-tidy modernize-*): |
| 14 | + * - modernize-use-nullptr: Use nullptr instead of NULL/0 |
| 15 | + * - modernize-use-override: Add override specifier to virtual functions |
| 16 | + * - modernize-use-auto: Use auto for iterator declarations |
| 17 | + * - modernize-loop-convert: Convert C-style loops to range-based |
| 18 | + * - modernize-use-default-member-init: Use default member initialization |
| 19 | + * |
| 20 | + * Tier 3 (AI Required - cppcheck): |
| 21 | + * - memleak: Memory leak detection |
| 22 | + * - nullPointer: Null pointer dereference |
| 23 | + * - uninitvar: Uninitialized variable use |
| 24 | + * - resourceLeak: File handles not closed |
| 25 | + * |
| 26 | + * DO NOT manually fix these issues - they are test fixtures. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <iostream> |
| 30 | +#include <vector> |
| 31 | +#include <string> |
| 32 | +#include <map> |
| 33 | +#include <fstream> |
| 34 | +#include <cstring> |
| 35 | +#include <cstdlib> |
| 36 | + |
| 37 | +// Tier 2 (modernize-use-override): Base class for testing override |
| 38 | +class BaseClass { |
| 39 | +public: |
| 40 | + virtual void process() { std::cout << "Base process" << std::endl; } |
| 41 | + virtual int calculate(int x) { return x; } |
| 42 | + virtual ~BaseClass() {} |
| 43 | +}; |
| 44 | + |
| 45 | +// Tier 2 (modernize-use-override): Missing override specifier |
| 46 | +class DerivedClass : public BaseClass { |
| 47 | +public: |
| 48 | + // modernize-use-override: should have override specifier |
| 49 | + virtual void process() { std::cout << "Derived process" << std::endl; } |
| 50 | + // modernize-use-override: should have override specifier |
| 51 | + virtual int calculate(int x) { return x * 2; } |
| 52 | + // modernize-use-override: destructor should have override |
| 53 | + virtual ~DerivedClass() {} |
| 54 | +}; |
| 55 | + |
| 56 | +// Another derived class with override issues |
| 57 | +class AnotherDerived : public BaseClass { |
| 58 | +public: |
| 59 | + // modernize-use-override: missing override |
| 60 | + virtual void process() { std::cout << "Another process" << std::endl; } |
| 61 | + // modernize-use-override: missing override |
| 62 | + virtual int calculate(int x) { return x + 10; } |
| 63 | +}; |
| 64 | + |
| 65 | +// Tier 1 (clang-format): Bad formatting - inconsistent spacing |
| 66 | +int badlyFormattedFunction(int a,int b,int c){ |
| 67 | +int result=a+b*c; |
| 68 | +return result; |
| 69 | +} |
| 70 | + |
| 71 | +// Tier 1 (clang-format): Missing spaces around operators |
| 72 | +int calculateSum(int x,int y){ |
| 73 | + return x+y*2-1; |
| 74 | +} |
| 75 | + |
| 76 | +// Tier 2 (modernize-use-nullptr): Use NULL instead of nullptr |
| 77 | +void nullPointerExamples() { |
| 78 | + // modernize-use-nullptr: should use nullptr |
| 79 | + int* ptr1 = NULL; |
| 80 | + char* ptr2 = NULL; |
| 81 | + void* ptr3 = 0; |
| 82 | + |
| 83 | + // modernize-use-nullptr: NULL in comparison |
| 84 | + if (ptr1 == NULL) { |
| 85 | + std::cout << "ptr1 is null" << std::endl; |
| 86 | + } |
| 87 | + |
| 88 | + // modernize-use-nullptr: 0 in comparison |
| 89 | + if (ptr2 != 0) { |
| 90 | + std::cout << "ptr2 is not null" << std::endl; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Tier 2 (modernize-use-nullptr): NULL in function arguments |
| 95 | +void processPointer(int* ptr) { |
| 96 | + if (ptr != NULL) { |
| 97 | + std::cout << *ptr << std::endl; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +void callWithNull() { |
| 102 | + // modernize-use-nullptr: passing NULL as argument |
| 103 | + processPointer(NULL); |
| 104 | +} |
| 105 | + |
| 106 | +// Tier 1 (clang-format): Poor brace placement and spacing |
| 107 | +void badBracePlacement(){ |
| 108 | +if(true){ |
| 109 | +std::cout<<"Cramped braces"<<std::endl; |
| 110 | +}else{ |
| 111 | +std::cout<<"No spaces"<<std::endl; |
| 112 | +} |
| 113 | +} |
| 114 | + |
| 115 | +// Tier 2 (modernize-use-auto): Iterator declarations |
| 116 | +void iteratorExamples() { |
| 117 | + std::vector<int> numbers = {1, 2, 3, 4, 5}; |
| 118 | + std::map<std::string, int> scores; |
| 119 | + scores["alice"] = 100; |
| 120 | + scores["bob"] = 95; |
| 121 | + |
| 122 | + // modernize-use-auto: verbose iterator declarations |
| 123 | + for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) { |
| 124 | + std::cout << *it << std::endl; |
| 125 | + } |
| 126 | + |
| 127 | + // modernize-use-auto: verbose const_iterator |
| 128 | + for (std::vector<int>::const_iterator it = numbers.cbegin(); it != numbers.cend(); ++it) { |
| 129 | + std::cout << *it << std::endl; |
| 130 | + } |
| 131 | + |
| 132 | + // modernize-use-auto: map iterator |
| 133 | + for (std::map<std::string, int>::iterator it = scores.begin(); it != scores.end(); ++it) { |
| 134 | + std::cout << it->first << ": " << it->second << std::endl; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// Tier 1 (clang-format): Inconsistent indentation |
| 139 | +void mixedIndentation() { |
| 140 | + int a = 1; |
| 141 | + int b = 2; |
| 142 | + int c = 3; |
| 143 | + int d = 4; |
| 144 | + std::cout << a << b << c << d << std::endl; |
| 145 | +} |
| 146 | + |
| 147 | +// Tier 3 (cppcheck memleak): Memory allocated but not freed |
| 148 | +void memoryLeakExample(int size) { |
| 149 | + // cppcheck: Memory leak - allocated memory never freed |
| 150 | + int* array = new int[size]; |
| 151 | + for (int i = 0; i < size; i++) { |
| 152 | + array[i] = i * 2; |
| 153 | + } |
| 154 | + std::cout << "Array processed" << std::endl; |
| 155 | + // Missing: delete[] array; |
| 156 | +} |
| 157 | + |
| 158 | +// Tier 3 (cppcheck memleak): Multiple memory leaks |
| 159 | +void multipleMemoryLeaks() { |
| 160 | + // cppcheck: Memory leak |
| 161 | + char* buffer1 = new char[100]; |
| 162 | + std::strcpy(buffer1, "Hello"); |
| 163 | + |
| 164 | + // cppcheck: Memory leak |
| 165 | + int* buffer2 = (int*)malloc(sizeof(int) * 50); |
| 166 | + buffer2[0] = 42; |
| 167 | + |
| 168 | + // cppcheck: Memory leak - conditional return without cleanup |
| 169 | + if (buffer2[0] > 40) { |
| 170 | + std::cout << "Returning early without freeing" << std::endl; |
| 171 | + return; // Memory leaks for buffer1 and buffer2 |
| 172 | + } |
| 173 | + |
| 174 | + // This code is unreachable if condition is true |
| 175 | + delete[] buffer1; |
| 176 | + free(buffer2); |
| 177 | +} |
| 178 | + |
| 179 | +// Tier 3 (cppcheck nullPointer): Potential null pointer dereference |
| 180 | +void nullPointerDereference(int* ptr) { |
| 181 | + // cppcheck: nullPointer - dereferencing before null check |
| 182 | + int value = *ptr; // Dereference before check |
| 183 | + |
| 184 | + if (ptr == NULL) { |
| 185 | + std::cout << "ptr is null" << std::endl; |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + std::cout << "Value: " << value << std::endl; |
| 190 | +} |
| 191 | + |
| 192 | +// Tier 3 (cppcheck uninitvar): Uninitialized variable |
| 193 | +int uninitializedVariable(bool flag) { |
| 194 | + int result; // cppcheck: uninitvar - may be used uninitialized |
| 195 | + |
| 196 | + if (flag) { |
| 197 | + result = 42; |
| 198 | + } |
| 199 | + // No else clause - result uninitialized if flag is false |
| 200 | + |
| 201 | + return result; |
| 202 | +} |
| 203 | + |
| 204 | +// Tier 1 (clang-format): Long line that should be wrapped |
| 205 | +void veryLongFunctionWithManyParameters(std::string parameterOne, std::string parameterTwo, std::string parameterThree, int parameterFour, int parameterFive, bool parameterSix) { std::cout << parameterOne << parameterTwo << parameterThree << parameterFour << parameterFive << parameterSix << std::endl; } |
| 206 | + |
| 207 | +// Tier 3 (cppcheck resourceLeak): File handle not closed |
| 208 | +void fileResourceLeak(const std::string& filename) { |
| 209 | + // cppcheck: resourceLeak - FILE* not closed |
| 210 | + FILE* file = fopen(filename.c_str(), "r"); |
| 211 | + if (file != NULL) { |
| 212 | + char buffer[256]; |
| 213 | + while (fgets(buffer, sizeof(buffer), file) != NULL) { |
| 214 | + std::cout << buffer; |
| 215 | + } |
| 216 | + // Missing: fclose(file); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +// Tier 3 (cppcheck resourceLeak): ifstream not closed |
| 221 | +void streamResourceLeak(const std::string& filename) { |
| 222 | + // cppcheck: resourceLeak - ifstream opened but never closed |
| 223 | + std::ifstream* file = new std::ifstream(filename); |
| 224 | + if (file->is_open()) { |
| 225 | + std::string line; |
| 226 | + while (std::getline(*file, line)) { |
| 227 | + std::cout << line << std::endl; |
| 228 | + } |
| 229 | + } |
| 230 | + // Missing: file->close() and delete file; |
| 231 | +} |
| 232 | + |
| 233 | +// Tier 1 (clang-format): Bad struct formatting |
| 234 | +struct BadlyFormattedStruct{ |
| 235 | +int x;int y; |
| 236 | +std::string name;bool enabled; |
| 237 | +}; |
| 238 | + |
| 239 | +// Tier 2 (modernize-use-nullptr): Class with NULL member init |
| 240 | +class ClassWithNullInit { |
| 241 | +public: |
| 242 | + // modernize-use-nullptr: NULL in member initialization |
| 243 | + ClassWithNullInit() : ptr(NULL), data(0) {} |
| 244 | + |
| 245 | + void setPtr(int* p) { |
| 246 | + // modernize-use-nullptr: NULL comparison |
| 247 | + if (p != NULL) { |
| 248 | + ptr = p; |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | +private: |
| 253 | + int* ptr; |
| 254 | + int* data; |
| 255 | +}; |
| 256 | + |
| 257 | +// Tier 3 (cppcheck): Use after free |
| 258 | +void useAfterFree() { |
| 259 | + int* ptr = new int(42); |
| 260 | + std::cout << "Value: " << *ptr << std::endl; |
| 261 | + delete ptr; |
| 262 | + // cppcheck: Using pointer after it was freed |
| 263 | + std::cout << "After delete: " << *ptr << std::endl; // Use after free! |
| 264 | +} |
| 265 | + |
| 266 | +// Tier 1 (clang-format): Multiple statements on one line |
| 267 | +void multipleStatements() { int a = 1; int b = 2; int c = 3; std::cout << a << b << c << std::endl; } |
| 268 | + |
| 269 | +// Tier 3 (cppcheck): Double free |
| 270 | +void doubleFree(bool condition) { |
| 271 | + int* ptr = new int(100); |
| 272 | + |
| 273 | + if (condition) { |
| 274 | + delete ptr; |
| 275 | + } |
| 276 | + |
| 277 | + // cppcheck: Double free if condition was true |
| 278 | + delete ptr; |
| 279 | +} |
| 280 | + |
| 281 | +// Tier 2 (modernize-loop-convert): C-style for loop that could be range-based |
| 282 | +void cStyleLoops() { |
| 283 | + std::vector<int> numbers = {10, 20, 30, 40, 50}; |
| 284 | + |
| 285 | + // modernize-loop-convert: should be range-based for loop |
| 286 | + for (size_t i = 0; i < numbers.size(); i++) { |
| 287 | + std::cout << numbers[i] << std::endl; |
| 288 | + } |
| 289 | + |
| 290 | + int array[] = {1, 2, 3, 4, 5}; |
| 291 | + // modernize-loop-convert: should be range-based for loop |
| 292 | + for (int i = 0; i < 5; i++) { |
| 293 | + std::cout << array[i] << std::endl; |
| 294 | + } |
| 295 | +} |
| 296 | + |
| 297 | +// Tier 1 (clang-format): Bad array initialization |
| 298 | +int badArrayInit[] = {1,2,3,4,5,6,7,8,9,10}; |
| 299 | + |
| 300 | +// Combined issues: formatting + modernize + potential cppcheck |
| 301 | +class CombinedIssues{ |
| 302 | +public: |
| 303 | +CombinedIssues():value(NULL),count(0){} |
| 304 | +void setValue(int*v){if(v!=NULL){value=v;}} |
| 305 | +int*getValue(){return value;} |
| 306 | +private: |
| 307 | +int*value;int count; |
| 308 | +}; |
| 309 | + |
| 310 | +int main() { |
| 311 | + // Basic usage to prevent "unused" warnings |
| 312 | + DerivedClass derived; |
| 313 | + derived.process(); |
| 314 | + std::cout << derived.calculate(5) << std::endl; |
| 315 | + |
| 316 | + std::cout << badlyFormattedFunction(1,2,3) << std::endl; |
| 317 | + std::cout << calculateSum(4,5) << std::endl; |
| 318 | + |
| 319 | + nullPointerExamples(); |
| 320 | + callWithNull(); |
| 321 | + badBracePlacement(); |
| 322 | + iteratorExamples(); |
| 323 | + mixedIndentation(); |
| 324 | + |
| 325 | + memoryLeakExample(10); |
| 326 | + |
| 327 | + int testVal = 42; |
| 328 | + nullPointerDereference(&testVal); |
| 329 | + |
| 330 | + std::cout << uninitializedVariable(true) << std::endl; |
| 331 | + |
| 332 | + BadlyFormattedStruct s; |
| 333 | + s.x=1;s.y=2; |
| 334 | + |
| 335 | + CombinedIssues combined; |
| 336 | + combined.setValue(&testVal); |
| 337 | + |
| 338 | + cStyleLoops(); |
| 339 | + |
| 340 | + return 0; |
| 341 | +} |
0 commit comments