diff --git a/README_make_constexpr.md b/README_make_constexpr.md new file mode 100644 index 0000000..d77bb40 --- /dev/null +++ b/README_make_constexpr.md @@ -0,0 +1,62 @@ +# Make Variables Constexpr Script + +This script converts `auto` variable declarations for `$break`, `$continue`, and `any` variables to `static constexpr` declarations in C++ code. + +## Purpose + +The script addresses issue #23 by automatically converting the following patterns: + +- `auto $break = Constants.Break;` → `static constexpr auto $break = Constants.Break;` +- `auto $continue = Constants.Continue;` → `static constexpr auto $continue = Constants.Continue;` +- `auto any = Constants.Any;` → `static constexpr auto any = Constants.Any;` +- `auto $break {Constants.Break};` → `static constexpr auto $break {Constants.Break};` +- `auto $continue {Constants.Continue};` → `static constexpr auto $continue {Constants.Continue};` +- `auto any {Constants.Any};` → `static constexpr auto any {Constants.Any};` + +## Usage + +```bash +# Process current directory +./make_constexpr.sh + +# Process specific directory +./make_constexpr.sh /path/to/cpp/project + +# Show help +./make_constexpr.sh --help +``` + +## What it does + +1. Searches for C++ files (.h, .hpp, .cpp, .cc) in the specified directory +2. Applies regex transformations to convert auto declarations to constexpr +3. Only modifies lines that exactly match the expected patterns +4. Preserves indentation and formatting +5. Reports which files were modified + +## Safety + +- Only converts specific patterns that match Constants.Break, Constants.Continue, and Constants.Any +- Does not modify other auto variable declarations +- Preserves original file formatting and indentation +- Shows clear output indicating which files were changed + +## Example + +Before: +```cpp +void someMethod() { + auto $break = Constants.Break; + auto $continue = Constants.Continue; + auto any = Constants.Any; +} +``` + +After: +```cpp +void someMethod() { + static constexpr auto $break = Constants.Break; + static constexpr auto $continue = Constants.Continue; + static constexpr auto any = Constants.Any; +} +``` \ No newline at end of file diff --git a/examples/test_files/sample1.h b/examples/test_files/sample1.h new file mode 100644 index 0000000..408a0d5 --- /dev/null +++ b/examples/test_files/sample1.h @@ -0,0 +1,26 @@ +#pragma once + +namespace Test { + class SampleClass { + public: + void Method1() { + static constexpr auto $break = Constants.Break; + static constexpr auto $continue = Constants.Continue; + static constexpr auto any = Constants.Any; + + // Some code using these variables + if (condition) { + return $break; + } + return $continue; + } + + void Method2() { + static constexpr auto $break {Constants.Break}; + static constexpr auto $continue {Constants.Continue}; + static constexpr auto any {Constants.Any}; + + // More code + } + }; +} \ No newline at end of file diff --git a/examples/test_files/sample2.cpp b/examples/test_files/sample2.cpp new file mode 100644 index 0000000..7871099 --- /dev/null +++ b/examples/test_files/sample2.cpp @@ -0,0 +1,24 @@ +#include "sample1.h" + +void GlobalFunction() { + static constexpr auto $break = Constants.Break; + static constexpr auto $continue = Constants.Continue; + static constexpr auto any = Constants.Any; + + // Test mixed patterns + if (something) { + static constexpr auto $break {Constants.Break}; + return $break; + } + + // This should NOT be changed (parameter) + auto someOtherVariable = SomeClass.SomeValue; +} + +class AnotherClass { +private: + void PrivateMethod() { + static constexpr auto any {Constants.Any}; + // Process with any + } +}; \ No newline at end of file diff --git a/make_constexpr.sh b/make_constexpr.sh new file mode 100755 index 0000000..9db7347 --- /dev/null +++ b/make_constexpr.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# Simple script to make $break, $continue, $any variables constexpr +# This script processes C++ header and source files + +set -e + +echo "Making \$break, \$continue, and 'any' variables constexpr..." +echo + +# Function to process a single file +process_file() { + local file="$1" + local temp_file="${file}.tmp" + + echo "Processing: $file" + + # Apply all transformations with sed + sed -e 's/^\([ \t]*\)auto \$break = Constants\.Break;/\1static constexpr auto $break = Constants.Break;/g' \ + -e 's/^\([ \t]*\)auto \$break {Constants\.Break};/\1static constexpr auto $break {Constants.Break};/g' \ + -e 's/^\([ \t]*\)auto \$continue = Constants\.Continue;/\1static constexpr auto $continue = Constants.Continue;/g' \ + -e 's/^\([ \t]*\)auto \$continue {Constants\.Continue};/\1static constexpr auto $continue {Constants.Continue};/g' \ + -e 's/^\([ \t]*\)auto any = Constants\.Any;/\1static constexpr auto any = Constants.Any;/g' \ + -e 's/^\([ \t]*\)auto any {Constants\.Any};/\1static constexpr auto any {Constants.Any};/g' \ + "$file" > "$temp_file" + + # Check if changes were made + if ! cmp -s "$file" "$temp_file"; then + mv "$temp_file" "$file" + echo " ✓ Updated" + return 0 + else + rm "$temp_file" + echo " - No changes needed" + return 1 + fi +} + +# Main processing +target_dir="${1:-.}" + +if [ ! -d "$target_dir" ]; then + echo "Error: Directory does not exist: $target_dir" + exit 1 +fi + +echo "Searching in directory: $target_dir" +echo + +total_files=0 +changed_files=0 + +# Process all C++ files +for file in $(find "$target_dir" -type f \( -name "*.h" -o -name "*.hpp" -o -name "*.cpp" -o -name "*.cc" \)); do + total_files=$((total_files + 1)) + if process_file "$file"; then + changed_files=$((changed_files + 1)) + fi +done + +echo +echo "Summary: $changed_files out of $total_files files were modified" + +if [ $changed_files -gt 0 ]; then + echo "✓ Conversion completed successfully!" +else + echo "- No files needed changes" +fi \ No newline at end of file