codanna 0.9.19

Code Intelligence for Large Language Models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/**
 * @file comprehensive.cpp
 * @brief Comprehensive C++ example for parser testing - diverse language constructs
 * @author Code Intelligence System
 * 
 * This file demonstrates key C++ language features with minimal repetition:
 * - Classes, inheritance, virtual functions
 * - Templates and generic programming
 * - Namespaces and scope resolution
 * - Modern C++ features (auto, lambda, smart pointers)
 * - Exception handling and RAII
 * - Operator overloading and special member functions
 */

#include <iostream>
#include <vector>
#include <memory>
#include <string>
#include <algorithm>
#include <functional>
#include <exception>

/**
 * @namespace geometry
 * @brief Namespace containing geometric shapes and operations
 */
namespace geometry {
    
    /**
     * @class Shape
     * @brief Abstract base class for geometric shapes
     * 
     * Demonstrates virtual functions, pure virtual methods,
     * and polymorphic interfaces in C++.
     */
    class Shape {
    public:
        /**
         * @brief Virtual destructor for proper cleanup
         */
        virtual ~Shape() = default;
        
        /**
         * @brief Pure virtual function for area calculation
         * @return Area of the shape
         */
        virtual double area() const = 0;
        
        /**
         * @brief Virtual function for perimeter calculation
         * @return Perimeter of the shape
         */
        virtual double perimeter() const { return 0.0; }
        
        /**
         * @brief Non-virtual function with default implementation
         */
        void display() const {
            std::cout << "Shape with area: " << area() << std::endl;
        }
    };
    
    /**
     * @class Circle
     * @brief Concrete implementation of circular shape
     * 
     * Demonstrates inheritance, constructor initialization lists,
     * and const member functions.
     */
    class Circle : public Shape {
    private:
        double radius_;  ///< Circle radius
        
    public:
        /**
         * @brief Constructor with initialization list
         * @param r Circle radius
         */
        explicit Circle(double r) : radius_(r) {}
        
        /**
         * @brief Override area calculation for circle
         */
        double area() const override {
            return 3.14159 * radius_ * radius_;
        }
        
        /**
         * @brief Override perimeter calculation for circle
         */
        double perimeter() const override {
            return 2 * 3.14159 * radius_;
        }
        
        /**
         * @brief Getter for radius
         */
        double radius() const noexcept { return radius_; }
    };
    
    /**
     * @class Rectangle
     * @brief Rectangle shape with multiple inheritance demonstration
     */
    class Rectangle : public Shape {
    private:
        double width_, height_;
        
    public:
        Rectangle(double w, double h) : width_(w), height_(h) {}
        
        double area() const override {
            return width_ * height_;
        }
        
        double perimeter() const override {
            return 2 * (width_ + height_);
        }
    };
    
} // namespace geometry

/**
 * @namespace utils
 * @brief Utility namespace with template functions and algorithms
 */
namespace utils {
    
    /**
     * @brief Template function for generic comparison
     * @tparam T Type of elements to compare
     * @param a First element
     * @param b Second element
     * @return Maximum of the two elements
     */
    template<typename T>
    constexpr T max(const T& a, const T& b) {
        return (a > b) ? a : b;
    }
    
    /**
     * @brief Template class for generic container operations
     * @tparam Container Container type
     * @tparam Predicate Predicate function type
     */
    template<class Container, class Predicate>
    auto filter(const Container& container, Predicate pred) -> std::vector<typename Container::value_type> {
        std::vector<typename Container::value_type> result;
        std::copy_if(container.begin(), container.end(), 
                    std::back_inserter(result), pred);
        return result;
    }
    
    /**
     * @brief Template specialization example
     */
    template<>
    constexpr int max<int>(const int& a, const int& b) {
        return (a > b) ? a : b;
    }
    
} // namespace utils

/**
 * @class ResourceManager
 * @brief RAII resource management demonstration
 * 
 * Shows constructor/destructor patterns, move semantics,
 * and proper resource management in C++.
 */
class ResourceManager {
private:
    std::unique_ptr<int[]> data_;
    size_t size_;
    
public:
    /**
     * @brief Constructor with resource allocation
     */
    explicit ResourceManager(size_t size) 
        : data_(std::make_unique<int[]>(size)), size_(size) {
        std::cout << "ResourceManager allocated " << size << " integers\n";
    }
    
    /**
     * @brief Move constructor
     */
    ResourceManager(ResourceManager&& other) noexcept 
        : data_(std::move(other.data_)), size_(other.size_) {
        other.size_ = 0;
    }
    
    /**
     * @brief Move assignment operator
     */
    ResourceManager& operator=(ResourceManager&& other) noexcept {
        if (this != &other) {
            data_ = std::move(other.data_);
            size_ = other.size_;
            other.size_ = 0;
        }
        return *this;
    }
    
    /**
     * @brief Deleted copy operations (move-only class)
     */
    ResourceManager(const ResourceManager&) = delete;
    ResourceManager& operator=(const ResourceManager&) = delete;
    
    /**
     * @brief Destructor with cleanup logging
     */
    ~ResourceManager() {
        if (size_ > 0) {
            std::cout << "ResourceManager deallocating " << size_ << " integers\n";
        }
    }
    
    /**
     * @brief Array access operator overload
     */
    int& operator[](size_t index) {
        return data_[index];
    }
    
    /**
     * @brief Const array access operator
     */
    const int& operator[](size_t index) const {
        return data_[index];
    }
    
    size_t size() const noexcept { return size_; }
};

/**
 * @class CustomException
 * @brief Custom exception class demonstration
 */
class CustomException : public std::exception {
private:
    std::string message_;
    
public:
    explicit CustomException(const std::string& msg) : message_(msg) {}
    
    const char* what() const noexcept override {
        return message_.c_str();
    }
};

/**
 * @brief Function demonstrating exception handling and RAII
 * @param risky_operation Flag to trigger exception
 */
void demonstrate_exceptions(bool risky_operation) {
    try {
        ResourceManager manager(10);
        
        if (risky_operation) {
            throw CustomException("Simulated error in operation");
        }
        
        // Use the resource
        for (size_t i = 0; i < manager.size(); ++i) {
            manager[i] = static_cast<int>(i * i);
        }
        
    } catch (const CustomException& e) {
        std::cerr << "Custom exception caught: " << e.what() << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Standard exception caught: " << e.what() << std::endl;
    }
    // RAII ensures ResourceManager destructor is called
}

/**
 * @brief Function demonstrating lambda expressions and algorithms
 */
void demonstrate_lambdas() {
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    // Lambda with capture by value
    int multiplier = 2;
    auto multiply = [multiplier](int x) { return x * multiplier; };
    
    // Lambda with capture by reference
    int sum = 0;
    auto accumulate = [&sum](int x) { sum += x; };
    
    // Generic lambda (C++14)
    auto generic_print = [](const auto& value) {
        std::cout << value << " ";
    };
    
    // Use lambdas with STL algorithms
    std::for_each(numbers.begin(), numbers.end(), accumulate);
    std::cout << "Sum: " << sum << std::endl;
    
    // Transform with lambda
    std::vector<int> doubled;
    std::transform(numbers.begin(), numbers.end(), 
                  std::back_inserter(doubled), multiply);
    
    std::cout << "Doubled: ";
    std::for_each(doubled.begin(), doubled.end(), generic_print);
    std::cout << std::endl;
}

/**
 * @brief Template function demonstrating perfect forwarding
 * @tparam T Type to be forwarded
 * @param value Value to forward
 */
template<typename T>
void perfect_forward(T&& value) {
    // Demonstrate perfect forwarding
    auto process = [](auto&& arg) {
        std::cout << "Processing: " << std::forward<decltype(arg)>(arg) << std::endl;
    };
    process(std::forward<T>(value));
}

/**
 * @brief Main function demonstrating various C++ features
 * @return Program exit code
 */
int main() {
    std::cout << "=== C++ Comprehensive Example ===" << std::endl;
    
    // Polymorphism demonstration
    std::vector<std::unique_ptr<geometry::Shape>> shapes;
    shapes.push_back(std::make_unique<geometry::Circle>(5.0));
    shapes.push_back(std::make_unique<geometry::Rectangle>(4.0, 6.0));
    
    for (const auto& shape : shapes) {
        shape->display();
    }
    
    // Template usage
    std::cout << "Max of 10 and 20: " << utils::max(10, 20) << std::endl;
    std::cout << "Max of 3.14 and 2.71: " << utils::max(3.14, 2.71) << std::endl;
    
    // Container filtering with templates
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    auto even_numbers = utils::filter(numbers, [](int x) { return x % 2 == 0; });
    
    std::cout << "Even numbers: ";
    for (int n : even_numbers) {
        std::cout << n << " ";
    }
    std::cout << std::endl;
    
    // Exception handling demonstration
    demonstrate_exceptions(false);  // Normal operation
    demonstrate_exceptions(true);   // Trigger exception
    
    // Lambda demonstration
    demonstrate_lambdas();
    
    // Perfect forwarding demonstration
    std::string text = "Hello, World!";
    perfect_forward(text);                    // lvalue
    perfect_forward(std::string("Temp"));     // rvalue
    
    // Auto type deduction
    auto automatic_int = 42;
    auto automatic_double = 3.14159;
    auto automatic_string = std::string("auto deduction");
    
    std::cout << "Auto deduced types: " 
              << automatic_int << ", " 
              << automatic_double << ", " 
              << automatic_string << std::endl;
    
    // Range-based for loops
    std::cout << "Numbers: ";
    for (const auto& num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

/**
 * @brief Color enumeration for testing enum extraction
 */
enum Color {
    RED,
    GREEN,
    BLUE
};

/**
 * @brief Point structure for testing struct extraction
 */
struct Point {
    int x;
    int y;
};

/**
 * @brief Status enum class for scoped enums
 */
enum class Status {
    SUCCESS,
    FAILURE,
    PENDING
};

/**
 * @brief Union for testing union_specifier
 */
union Data {
    int integer;
    float floating;
    char character;
};

/**
 * @brief Type alias using typedef
 */
typedef unsigned long ulong;
typedef Point* PointPtr;

/**
 * @brief Using declarations to bring symbols into scope
 */
using std::cout;
using std::endl;
using std::vector;

/**
 * @brief Modern type alias using alias declaration
 */
using StringVector = std::vector<std::string>;
using IntPtr = std::unique_ptr<int>;

/**
 * @class OperatorExample
 * @brief Demonstrates operator overloading
 */
class OperatorExample {
private:
    int value_;

public:
    /**
     * @brief Constructor with parameter
     */
    explicit OperatorExample(int val) : value_(val) {}

    /**
     * @brief Copy constructor
     */
    OperatorExample(const OperatorExample& other) : value_(other.value_) {}

    /**
     * @brief Destructor
     */
    ~OperatorExample() {
        // Cleanup
    }

    /**
     * @brief Operator overload: addition
     */
    OperatorExample operator+(const OperatorExample& other) const {
        return OperatorExample(value_ + other.value_);
    }

    /**
     * @brief Operator overload: assignment
     */
    OperatorExample& operator=(const OperatorExample& other) {
        if (this != &other) {
            value_ = other.value_;
        }
        return *this;
    }

    /**
     * @brief Operator overload: equality
     */
    bool operator==(const OperatorExample& other) const {
        return value_ == other.value_;
    }

    /**
     * @brief Operator overload: stream output
     */
    friend std::ostream& operator<<(std::ostream& os, const OperatorExample& obj) {
        os << obj.value_;
        return os;
    }

    int getValue() const { return value_; }
};

/**
 * @brief Template instantiation examples
 */
void template_instantiation_examples() {
    // Explicit template instantiation
    std::vector<int> int_vector;
    std::vector<std::string> string_vector;

    // Template class instantiation
    std::unique_ptr<geometry::Circle> circle_ptr = std::make_unique<geometry::Circle>(10.0);
    std::unique_ptr<geometry::Rectangle> rect_ptr = std::make_unique<geometry::Rectangle>(5.0, 10.0);

    // Template function instantiation
    auto max_int = utils::max<int>(10, 20);
    auto max_double = utils::max<double>(3.14, 2.71);
}

/**
 * @brief Call expression examples for parser testing
 */
void call_expression_examples() {
    // Simple call_expression
    demonstrate_lambdas();
    demonstrate_exceptions(true);

    // Member function calls (field_expression)
    geometry::Circle circle(5.0);
    double area = circle.area();
    double perimeter = circle.perimeter();
    circle.display();

    // Pointer member calls (field_expression with ->)
    geometry::Circle* circle_ptr = new geometry::Circle(3.0);
    double ptr_area = circle_ptr->area();
    circle_ptr->display();
    delete circle_ptr;

    // Scoped function calls (scoped_identifier)
    int max_val = utils::max(10, 20);
    int specialized = utils::max<int>(30, 40);

    // More scoped_identifier examples
    std::string str = "test";
    std::vector<int> vec;
    std::cout << "Using scoped_identifier" << std::endl;
    std::make_unique<int>(42);
    geometry::Circle::radius();  // scoped static member access

    // Nested member calls (multiple field_expressions)
    std::unique_ptr<geometry::Shape> shape = std::make_unique<geometry::Circle>(7.0);
    shape->display();

    // Static member function calls (qualified_identifier in call context)
    std::cout << "Test output" << std::endl;
}

/**
 * @class TestClass
 * @brief Class with external method definitions for qualified_identifier testing
 */
class TestClass {
public:
    void methodA();
    void methodB() const;
    static void staticMethod();
};

// Method implementation with qualified_identifier
void TestClass::methodA() {
    std::cout << "TestClass::methodA implementation" << std::endl;
}

// Const method with qualified_identifier
void TestClass::methodB() const {
    std::cout << "TestClass::methodB const implementation" << std::endl;
}

// Static method with qualified_identifier
void TestClass::staticMethod() {
    std::cout << "TestClass::staticMethod implementation" << std::endl;
}