Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions README_make_constexpr.md
Original file line number Diff line number Diff line change
@@ -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;
}
```
26 changes: 26 additions & 0 deletions examples/test_files/sample1.h
Original file line number Diff line number Diff line change
@@ -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
}
};
}
24 changes: 24 additions & 0 deletions examples/test_files/sample2.cpp
Original file line number Diff line number Diff line change
@@ -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
}
};
68 changes: 68 additions & 0 deletions make_constexpr.sh
Original file line number Diff line number Diff line change
@@ -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