diff --git a/.astylerc b/.astylerc new file mode 100644 index 00000000..25414900 --- /dev/null +++ b/.astylerc @@ -0,0 +1,15 @@ +# style +--style=kr +--indent=spaces=4 +--indent-namespaces +--lineend=linux +--min-conditional-indent=0 + +# options +--pad-header +--unpad-paren +--suffix=none +--convert-tabs +--attach-inlines +--attach-classes +--attach-namespaces diff --git a/runastyle b/runastyle index 64298273..fc7094c5 100755 --- a/runastyle +++ b/runastyle @@ -4,20 +4,34 @@ # different versions might have different output (this has happened in # the past). -# If project management wishes to take a newer astyle version into use -# just change this string to match the start of astyle version string. -ASTYLE_VERSION="Artistic Style Version 3.0.1" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +cd $SCRIPT_DIR + +# To require a newer astyle version, update ASTYLE_VERSION below. +# ASTYLE_VERSION_STR is then constructed to match the beginning of the +# version string reported by "astyle --version". +ASTYLE_VERSION="3.4.13" +ASTYLE_VERSION_STR="Artistic Style Version ${ASTYLE_VERSION}" ASTYLE="astyle" -DETECTED_VERSION=`$ASTYLE --version 2>&1` -if [[ "$DETECTED_VERSION" != ${ASTYLE_VERSION}* ]]; then - echo "You should use: ${ASTYLE_VERSION}"; - echo "Detected: ${DETECTED_VERSION}" - exit 1; +if command -v astyle &> /dev/null; then + ASTYLE="astyle" +elif command -v uvx &> /dev/null; then + ASTYLE="uvx --quiet ${ASTYLE}==${ASTYLE_VERSION}" +elif command -v pipx &> /dev/null; then + ASTYLE="pipx run --quiet ${ASTYLE}==${ASTYLE_VERSION}" +else + echo "Neither astyle, uvx, nor pipx found in PATH" + exit 1 fi -style="--style=kr --indent=spaces=4 --indent-namespaces --lineend=linux --min-conditional-indent=0" -options="--options=none --pad-header --unpad-paren --suffix=none --convert-tabs --attach-inlines --attach-classes --attach-namespaces" +DETECTED_VERSION_STR=`$ASTYLE --version 2>&1` +if [[ "$DETECTED_VERSION_STR" != ${ASTYLE_VERSION_STR}* ]]; then + echo "You should use: ${ASTYLE_VERSION_STR}"; + echo "Detected: ${DETECTED_VERSION_STR}" + exit 1; +fi -$ASTYLE $style $options *.cpp -$ASTYLE $style $options *.h +# Run astyle with the project config +$ASTYLE --project *.h,*.cpp diff --git a/runastyle.bat b/runastyle.bat index b8f11561..905e898e 100644 --- a/runastyle.bat +++ b/runastyle.bat @@ -1,26 +1,53 @@ -@REM Script to run AStyle on the sources -@REM The version check in this script is used to avoid commit battles -@REM between different developers that use different astyle versions as -@REM different versions might have different output (this has happened in -@REM the past). +@ECHO off +REM Script to run AStyle on the sources +REM The version check in this script is used to avoid commit battles +REM between different developers that use different astyle versions as +REM different versions might have different output (this has happened in +REM the past). -@REM If project management wishes to take a newer astyle version into use -@REM just change this string to match the start of astyle version string. -@SET ASTYLE_VERSION="Artistic Style Version 3.0.1" -@SET ASTYLE="astyle" +REM Get the directory of the script +SET SCRIPT_DIR=%~dp0 -@SET DETECTED_VERSION="" -@FOR /F "tokens=*" %%i IN ('%ASTYLE% --version') DO SET DETECTED_VERSION=%%i -@ECHO %DETECTED_VERSION% | FINDSTR /B /C:%ASTYLE_VERSION% > nul && ( - ECHO "%DETECTED_VERSION%" matches %ASTYLE_VERSION% +REM Change to that directory +CD /d %SCRIPT_DIR% + +REM To require a newer astyle version, update ASTYLE_VERSION below. +REM ASTYLE_VERSION_STR is then constructed to match the beginning of the +REM version string reported by "astyle --version". +SET ASTYLE_VERSION="3.4.13" +SET ASTYLE_VERSION_STR="Artistic Style Version %ASTYLE_VERSION%" + +REM Prefer system astyle; else try uvx; else pipx +SET "ASTYLE=astyle" +where astyle >nul 2>nul +IF %errorlevel% neq 0 ( + where uvx >nul 2>nul + IF %errorlevel% eq 0 ( + SET "ASTYLE=uvx --quiet astyle==%ASTYLE_VERSION%" + ) ELSE ( + where pipx >nul 2>nul + IF %errorlevel% eq 0 ( + SET "ASTYLE=pipx run --quiet astyle==%ASTYLE_VERSION%" + ) ELSE ( + ECHO ERROR: Neither astyle, uvx, nor pipx found in PATH. + GOTO EXIT_ERROR + ) + ) +) + +SET DETECTED_VERSION_STR="" +FOR /F "tokens=*" %%i IN ('%ASTYLE% --version') DO SET DETECTED_VERSION_STR=%%i +ECHO %DETECTED_VERSION_STR% | FINDSTR /B /C:%ASTYLE_VERSION_STR% > nul && ( + ECHO "%DETECTED_VERSION_STR%" matches %ASTYLE_VERSION_STR% ) || ( - ECHO You should use: %ASTYLE_VERSION% - ECHO Detected: "%DETECTED_VERSION%" + ECHO You should use: %ASTYLE_VERSION_STR% + ECHO Detected: "%DETECTED_VERSION_STR%" GOTO EXIT_ERROR ) -@SET STYLE=--style=kr --indent=spaces=4 --indent-namespaces --lineend=linux --min-conditional-indent=0 -@SET OPTIONS=--pad-header --unpad-paren --suffix=none --convert-tabs --attach-inlines --attach-classes --attach-namespaces +REM Run astyle with the project config +%ASTYLE% --project *.h,*.cpp +GOTO :EOF -%ASTYLE% %STYLE% %OPTIONS% *.cpp -%ASTYLE% %STYLE% %OPTIONS% *.h \ No newline at end of file +:EXIT_ERROR +EXIT /b 1 diff --git a/simplecpp.cpp b/simplecpp.cpp index fd327549..87c92aca 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -761,18 +761,18 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, while (stream.good() && ch != '\n') { currentToken += ch; ch = stream.readChar(); - if(ch == '\\') { + if (ch == '\\') { TokenString tmp; char tmp_ch = ch; - while((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) { + while ((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) { tmp += tmp_ch; tmp_ch = stream.readChar(); } - if(!stream.good()) { + if (!stream.good()) { break; } - if(tmp_ch != '\n') { + if (tmp_ch != '\n') { currentToken += tmp; } else { const TokenString check_portability = currentToken + tmp; diff --git a/test.cpp b/test.cpp index ccb653ca..09c2ba92 100644 --- a/test.cpp +++ b/test.cpp @@ -438,14 +438,14 @@ static void comment_multiline() ASSERT_EQUALS("\n\nvoid f ( ) {", preprocess(code)); const char code1[] = "#define ABC {// \\\r\n" - "}\n" - "void f() ABC\n"; + "}\n" + "void f() ABC\n"; ASSERT_EQUALS("\n\nvoid f ( ) {", preprocess(code1)); const char code2[] = "#define A 1// \\\r" - "\r" - "2\r" - "A\r"; + "\r" + "2\r" + "A\r"; ASSERT_EQUALS("\n\n2\n1", preprocess(code2)); const char code3[] = "void f() {// \\ \n}\n"; @@ -2100,7 +2100,7 @@ static void circularInclude() "#define TEST_H\n" "#include \"a/a.h\"\n" "#endif\n" - ; + ; cache.insert({path, makeTokenList(code, files, path)}); } @@ -2111,7 +2111,7 @@ static void circularInclude() "#define A_H\n" "#include \"../test.h\"\n" "#endif\n" - ; + ; cache.insert({path, makeTokenList(code, files, path)}); } @@ -3176,7 +3176,8 @@ static void fuzz_crash() "n\n"; (void)preprocess(code, simplecpp::DUI()); // do not crash } - { // #346 + { + // #346 const char code[] = "#define foo(intp)f##oo(intp\n" "foo(f##oo(intp))\n"; (void)preprocess(code, simplecpp::DUI()); // do not crash