Add mechanisms to reformant and check code style (#128)

Use an internal version of astyle (astyle 3.0). Scripts taken from QGIS.
astyle.options from https://github.com/uclouvain/openjpeg/issues/128

scripts/prepare-commit.sh can be used locally to automatically reformat
edited files.

Travis-CI will run scripts/verify-indentation.sh to verify committed files.
This commit is contained in:
Even Rouault 2017-05-09 15:28:09 +02:00
parent 8650b70e06
commit d4e54e9f35
26 changed files with 20242 additions and 21 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ cmake_install.cmake
/src/bin/common/opj_apps_config.h
/src/lib/openjp2/opj_config.h
/src/lib/openjp2/opj_config_private.h
scripts/opjstyle*
# Ignore directories made by `make`.
/bin/

View File

@ -1,4 +1,4 @@
language: c
language: cpp
matrix:
include:
@ -6,20 +6,32 @@ matrix:
compiler: clang
env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Release OPJ_CI_INCLUDE_IF_DEPLOY=1
- os: linux
compiler: gcc
compiler: clang-3.8
env: OPJ_CI_CC=clang-3.8 OPJ_CI_CXX=clang-3.8 OPJ_CI_CHECK_STYLE=1 OPJ_CI_SKIP_TESTS=1
addons:
apt:
sources:
- llvm-toolchain-precise-3.8
- ubuntu-toolchain-r-test
packages:
- clang-3.8
- flip
- os: linux
compiler: g++
env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Release OPJ_CI_INCLUDE_IF_DEPLOY=1 OPJ_CI_PERF_TESTS=1
- os: linux
compiler: gcc
compiler: g++
env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Release OPJ_NUM_THREADS=2
- os: linux
compiler: gcc
compiler: g++
env: OPJ_CI_ARCH=i386 OPJ_CI_BUILD_CONFIGURATION=Release
addons:
apt:
packages:
- gcc-multilib
- g++-multilib
- os: linux
compiler: gcc
compiler: g++
env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Debug OPJ_CI_PROFILE=1
addons:
apt:
@ -30,7 +42,7 @@ matrix:
env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Debug OPJ_CI_ASAN=1
- os: linux
compiler: clang-3.8
env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Release OPJ_CI_PERF_TESTS=1
env: OPJ_CI_CC=clang-3.8 OPJ_CI_CXX=clang-3.8 OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Release OPJ_CI_PERF_TESTS=1
addons:
apt:
sources:
@ -39,18 +51,8 @@ matrix:
packages:
- clang-3.8
- os: linux
compiler: x86_64-w64-mingw32-gcc
env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Release
addons:
apt:
packages:
- gcc-mingw-w64-base
- binutils-mingw-w64-x86-64
- gcc-mingw-w64-x86-64
- gcc-mingw-w64
- os: linux
compiler: x86_64-w64-mingw32-gcc
env: OPJ_CI_ARCH=i386 OPJ_CI_BUILD_CONFIGURATION=Release
compiler: x86_64-w64-mingw32-g++
env: OPJ_CI_CC=x86_64-w64-mingw32-gcc OPJ_CI_CXX=x86_64-w64-mingw32-g++ OPJ_CI_ARCH=i386 OPJ_CI_BUILD_CONFIGURATION=Release
addons:
apt:
packages:
@ -58,15 +60,30 @@ matrix:
- binutils-mingw-w64-i686
- gcc-mingw-w64-i686
- gcc-mingw-w64
- g++-mingw-w64-i686
- gcc-multilib
- g++-multilib
- os: linux
compiler: gcc-4.8
env: OPJ_CI_ABI_CHECK=1
compiler: x86_64-w64-mingw32-g++
env: OPJ_CI_CC=x86_64-w64-mingw32-gcc OPJ_CI_CXX=x86_64-w64-mingw32-g++ OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Release
addons:
apt:
packages:
- gcc-mingw-w64-base
- binutils-mingw-w64-x86-64
- gcc-mingw-w64-x86-64
- gcc-mingw-w64
- g++-mingw-w64-x86-64
- os: linux
compiler: g++-4.8
env: OPJ_CI_CC=gcc-4.8 OPJ_CI_CXX=g++-4.8 OPJ_CI_ABI_CHECK=1
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.8
- g++-4.8
- libelf-dev
- elfutils
- texinfo

View File

@ -24,7 +24,7 @@ endif()
#string(TOLOWER ${OPENJPEG_NAMESPACE} OPENJPEG_LIBRARY_NAME)
set(OPENJPEG_LIBRARY_NAME openjp2)
project(${OPENJPEG_NAMESPACE} C)
project(${OPENJPEG_NAMESPACE})
# Do full dependency headers.
include_regular_expression("^.*$")
@ -386,3 +386,6 @@ if(BUILD_PKGCONFIG_FILES)
endif()
#-----------------------------------------------------------------------------
# build our version of astyle
SET (WITH_ASTYLE FALSE CACHE BOOL "If you plan to contribute you should reindent with scripts/prepare-commit.sh (using 'our' astyle)")

View File

@ -31,6 +31,7 @@ Main available cmake flags:
* To build the shared libraries and links the executables against it: '-DBUILD\_SHARED\_LIBS:bool=on' (default: 'ON')
> Note: when using this option, static libraries are not built and executables are dynamically linked.
* To build the CODEC executables: '-DBUILD\_CODEC:bool=on' (default: 'ON')
* To build opjstyle (internal version of astyle) for OpenJPEG development: '-DWITH_ASTYLE=ON'
* [OBSOLETE] To build the MJ2 executables: '-DBUILD\_MJ2:bool=on' (default: 'OFF')
* [OBSOLETE] To build the JPWL executables and JPWL library: '-DBUILD\_JPWL:bool=on' (default: 'OFF')
* [OBSOLETE] To build the JPIP client (java compiler recommended) library and executables: '-DBUILD\_JPIP:bool=on' (default: 'OFF')
@ -62,6 +63,11 @@ Note 4 : On MacOS, if it does not work, try adding the following flag to the cma
You can use cmake to generate the project files for the IDE you are using (VC2010, XCode, etc).
Type 'cmake --help' for available generators on your platform.
# Modifying OpenJPEG
Before committing changes, run:
scripts/prepare-commit.sh
# Using OpenJPEG
To use openjpeg exported cmake file, simply create your application doing:

View File

@ -49,6 +49,7 @@ The library is developed and maintained by the Image and Signal Processing Group
* doc: doxygen documentation setup file and man pages
* tests: configuration files and utilities for the openjpeg test suite. All test images are located in [openjpeg-data](https://github.com/uclouvain/openjpeg-data) repository.
* cmake: cmake related files
* scripts: scripts for developers
See [LICENSE][link-license] for license and copyright information.

11
scripts/astyle.options Normal file
View File

@ -0,0 +1,11 @@
--convert-tabs
--lineend=linux
--indent=spaces=4
--style=kr
--add-brackets
--max-code-length=80
--break-after-logical
--pad-header
--pad-oper
--unpad-paren
--suffix=none

117
scripts/astyle.sh Executable file
View File

@ -0,0 +1,117 @@
#!/bin/bash
###########################################################################
# astyle.sh
# ---------------------
# Date : August 2008
# Copyright : (C) 2008 by Juergen E. Fischer
# Email : jef at norbit dot de
###########################################################################
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
###########################################################################
for ASTYLE in ${OPJSTYLE} $(dirname $0)/opjstyle $(dirname $0)/RelWithDebInfo/opjstyle
do
if type -p $ASTYLE >/dev/null; then
break
fi
ASTYLE=
done
if [ -z "$ASTYLE" ]; then
echo "opjstyle not found - please enable WITH_ASTYLE in cmake and build it" >&2
exit 1
fi
if type -p tput >/dev/null; then
elcr="$ASTYLEPROGRESS$(tput el)$(tput cr)"
else
elcr="$ASTYLEPROGRESS \r"
fi
if ! type -p flip >/dev/null; then
if type -p dos2unix >/dev/null; then
flip() {
dos2unix -k $2
}
else
echo "flip not found" >&2
flip() {
:
}
fi
fi
if ! type -p autopep8 >/dev/null; then
echo "autopep8 not found" >&2
autopep8() {
:
}
fi
ASTYLEOPTS=$(dirname $0)/astyle.options
if type -p cygpath >/dev/null; then
ASTYLEOPTS="$(cygpath -w $ASTYLEOPTS)"
fi
set -e
astyleit() {
$ASTYLE --options="$ASTYLEOPTS" "$1"
#modified=$1.unify_includes_modified
#cp "$1" "$modified"
#scripts/unify_includes.pl "$modified"
#scripts/doxygen_space.pl "$modified"
#diff "$1" "$modified" >/dev/null || mv "$modified" "$1"
#rm -f "$modified"
}
for f in "$@"; do
case "$f" in
thirdparty/*)
echo -ne "$f skipped $elcr"
continue
;;
*.cpp|*.h|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.hpp)
if [ -x "$f" ]; then
chmod a-x "$f"
fi
cmd=astyleit
;;
*.py)
#cmd="autopep8 --in-place --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701"
echo -ne "Formatting $f $elcr"
cmd="autopep8 --in-place --ignore=E261,E265,E402,E501"
;;
*)
echo -ne "$f skipped $elcr"
continue
;;
esac
if ! [ -f "$f" ]; then
echo "$f not found" >&2
continue
fi
if [[ -f $f && `head -c 3 $f` == $'\xef\xbb\xbf' ]]; then
mv $f $f.bom
tail -c +4 $f.bom > $f
echo "removed BOM from $f"
fi
modified=$f.flip_modified
cp "$f" "$modified"
flip -ub "$modified"
diff "$f" "$modified" >/dev/null || mv "$modified" "$f"
rm -f "$modified"
eval "$cmd '$f'"
done

112
scripts/prepare-commit.sh Executable file
View File

@ -0,0 +1,112 @@
#!/usr/bin/env bash
###########################################################################
# prepare-commit.sh
# ---------------------
# Date : August 2008
# Copyright : (C) 2008 by Juergen E. Fischer
# Email : jef at norbit dot de
###########################################################################
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
###########################################################################
TOPLEVEL=$(git rev-parse --show-toplevel)
PATH=$TOPLEVEL/scripts:$PATH
cd $TOPLEVEL
# GNU prefix command for mac os support (gsed, gsplit)
GP=
if [[ "$OSTYPE" =~ darwin* ]]; then
GP=g
fi
if ! type -p astyle.sh >/dev/null; then
echo astyle.sh not found
exit 1
fi
if ! type -p colordiff >/dev/null; then
colordiff()
{
cat "$@"
}
fi
if [ "$1" = "-c" ]; then
echo "Cleaning..."
remove_temporary_files.sh
fi
set -e
# determine changed files
MODIFIED=$(git status --porcelain| ${GP}sed -ne "s/^ *[MA] *//p" | sort -u)
#MODIFIED=$(find src -name "*.c")
if [ -z "$MODIFIED" ]; then
echo nothing was modified
exit 0
fi
# save original changes
REV=$(git log -n1 --pretty=%H)
git diff >sha-$REV.diff
ASTYLEDIFF=astyle.$REV.diff
>$ASTYLEDIFF
# reformat
i=0
N=$(echo $MODIFIED | wc -w)
for f in $MODIFIED; do
(( i++ )) || true
case "$f" in
thirdparty/*)
echo $f skipped
continue
;;
*.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py)
;;
*)
continue
;;
esac
m=$f.$REV.prepare
cp $f $m
ASTYLEPROGRESS=" [$i/$N]" astyle.sh $f
if diff -u $m $f >>$ASTYLEDIFF; then
# no difference found
rm $m
fi
done
if [ -s "$ASTYLEDIFF" ]; then
if tty -s; then
# review astyle changes
colordiff <$ASTYLEDIFF | less -r
else
echo "Files changed (see $ASTYLEDIFF)"
fi
exit 1
else
rm $ASTYLEDIFF
fi
# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached HEAD --
exit 0
# vim: set ts=8 noexpandtab :

View File

@ -0,0 +1,41 @@
#!/bin/bash
###########################################################################
# remove_git_confict_files.sh
# ---------------------
# Date : April 2012
# Copyright : (C) 2012 by Tim Sutton
# Email : tim at kartoza dot com
###########################################################################
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
###########################################################################
#
# A simple script to get rid of QGIS related temporary files left in
# your QGIS source folder by git
# Tim Sutton, May 2008
find . \
\( \
-name "*.orig" \
-o -name "*.prepare" \
-o -name "*.sortinc" \
-o -name "*.unify_includes_modified" \
-o -name "*.nocopyright" \
-o -name "astyle*.diff" \
-o -name "sha-*.diff" \
-o -name "*.astyle" \
-o -name "sha*.diff" \
-o -name "*.bom" \
-o -name "*.bak" \
-o -name "*.rej" \
-o -name "*.orig" \
-o -name "*.new" \
-o -name "*~" \
\) \
-print \
-delete

79
scripts/verify-indentation.sh Executable file
View File

@ -0,0 +1,79 @@
#!/bin/bash
cd $(git rev-parse --show-toplevel)
export PATH=$PATH:$PWD/scripts
if [ -z "$TRAVIS_COMMIT_RANGE" ]; then
echo "No commit range given"
exit 0
fi
if ! type -p astyle.sh >/dev/null; then
echo astyle.sh not found
exit 1
fi
set -e
ASTYLEDIFF=/tmp/astyle.diff
>$ASTYLEDIFF
if [[ ! -z $TRAVIS_PULL_REQUEST_BRANCH ]]; then
# if on a PR, just analyse the changed files
echo "TRAVIS PR BRANCH: $TRAVIS_PULL_REQUEST_BRANCH"
FILES=$(git diff --diff-filter=AM --name-only $(git merge-base HEAD master) | tr '\n' ' ' )
elif [[ ! -z $TRAVIS_COMMIT_RANGE ]]; then
echo "TRAVIS COMMIT RANGE: $TRAVIS_COMMIT_RANGE"
FILES=$(git diff --diff-filter=AM --name-only ${TRAVIS_COMMIT_RANGE/.../..} | tr '\n' ' ' )
fi
for f in $FILES; do
if ! [ -f "$f" ]; then
echo "$f was removed." >>/tmp/ctest-important.log
continue
fi
echo "Checking $f" >>/tmp/ctest-important.log
case "$f" in
thirdparty*)
echo "$f skipped"
continue
;;
*.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py)
;;
*)
continue
;;
esac
m="$f.prepare"
cp "$f" "$m"
astyle.sh "$f"
if diff -u "$m" "$f" >>$ASTYLEDIFF; then
rm "$m"
else
echo "File $f needs indentation"
fi
done
if [ -s "$ASTYLEDIFF" ]; then
echo
echo "Required indentation updates:"
cat "$ASTYLEDIFF"
cat <<EOF
Tips to prevent and resolve:
* Enable WITH_ASTYLE in your cmake configuration to format C++ code
* Install autopep8 (>= 1.2.1) to format python code
* Use "scripts/astyle.sh file" to fix the now badly indented files
* Consider using scripts/prepare-commit.sh as pre-commit hook to avoid this
in the future (ln -s scripts/prepare-commit.sh .git/hooks/pre-commit) or
run it manually before each commit.
EOF
exit 1
fi

View File

@ -116,3 +116,9 @@ else(BUILD_THIRDPARTY)
endif(LCMS_FOUND)
endif(LCMS2_FOUND)
endif(BUILD_THIRDPARTY)
#------------
IF (WITH_ASTYLE)
ADD_SUBDIRECTORY(astyle)
ENDIF(WITH_ASTYLE)

3659
thirdparty/astyle/ASBeautifier.cpp vendored Executable file

File diff suppressed because it is too large Load Diff

797
thirdparty/astyle/ASEnhancer.cpp vendored Executable file
View File

@ -0,0 +1,797 @@
// ASEnhancer.cpp
// Copyright (c) 2017 by Jim Pattee <jimp03@email.com>.
// This code is licensed under the MIT License.
// License.md describes the conditions under which this software may be distributed.
//-----------------------------------------------------------------------------
// headers
//-----------------------------------------------------------------------------
#include "astyle.h"
//-----------------------------------------------------------------------------
// astyle namespace
//-----------------------------------------------------------------------------
namespace astyle {
//
//-----------------------------------------------------------------------------
// ASEnhancer class
//-----------------------------------------------------------------------------
/**
* ASEnhancer constructor
*/
ASEnhancer::ASEnhancer()
{
}
/**
* Destructor of ASEnhancer
*/
ASEnhancer::~ASEnhancer()
{
}
/**
* initialize the ASEnhancer.
*
* init() is called each time an ASFormatter object is initialized.
*/
void ASEnhancer::init(int _fileType,
int _indentLength,
int _tabLength,
bool _useTabs,
bool _forceTab,
bool _namespaceIndent,
bool _caseIndent,
bool _preprocBlockIndent,
bool _preprocDefineIndent,
bool _emptyLineFill,
vector<const pair<const string, const string>* >* _indentableMacros)
{
// formatting variables from ASFormatter and ASBeautifier
ASBase::init(_fileType);
indentLength = _indentLength;
tabLength = _tabLength;
useTabs = _useTabs;
forceTab = _forceTab;
namespaceIndent = _namespaceIndent;
caseIndent = _caseIndent;
preprocBlockIndent = _preprocBlockIndent;
preprocDefineIndent = _preprocDefineIndent;
emptyLineFill = _emptyLineFill;
indentableMacros = _indentableMacros;
quoteChar = '\'';
// unindent variables
lineNumber = 0;
braceCount = 0;
isInComment = false;
isInQuote = false;
switchDepth = 0;
eventPreprocDepth = 0;
lookingForCaseBrace = false;
unindentNextLine = false;
shouldUnindentLine = false;
shouldUnindentComment = false;
// switch struct and vector
sw.switchBraceCount = 0;
sw.unindentDepth = 0;
sw.unindentCase = false;
switchStack.clear();
// other variables
nextLineIsEventIndent = false;
isInEventTable = false;
nextLineIsDeclareIndent = false;
isInDeclareSection = false;
}
/**
* additional formatting for line of source code.
* every line of source code in a source code file should be sent
* one after the other to this function.
* indents event tables
* unindents the case blocks
*
* @param line the original formatted line will be updated if necessary.
*/
void ASEnhancer::enhance(string& line, bool isInNamespace, bool isInPreprocessor, bool isInSQL)
{
shouldUnindentLine = true;
shouldUnindentComment = false;
lineNumber++;
// check for beginning of event table
if (nextLineIsEventIndent)
{
isInEventTable = true;
nextLineIsEventIndent = false;
}
// check for beginning of SQL declare section
if (nextLineIsDeclareIndent)
{
isInDeclareSection = true;
nextLineIsDeclareIndent = false;
}
if (line.length() == 0
&& !isInEventTable
&& !isInDeclareSection
&& !emptyLineFill)
return;
// test for unindent on attached braces
if (unindentNextLine)
{
sw.unindentDepth++;
sw.unindentCase = true;
unindentNextLine = false;
}
// parse characters in the current line
parseCurrentLine(line, isInPreprocessor, isInSQL);
// check for SQL indentable lines
if (isInDeclareSection)
{
size_t firstText = line.find_first_not_of(" \t");
if (firstText == string::npos || line[firstText] != '#')
indentLine(line, 1);
}
// check for event table indentable lines
if (isInEventTable
&& (eventPreprocDepth == 0
|| (namespaceIndent && isInNamespace)))
{
size_t firstText = line.find_first_not_of(" \t");
if (firstText == string::npos || line[firstText] != '#')
indentLine(line, 1);
}
if (shouldUnindentComment && sw.unindentDepth > 0)
unindentLine(line, sw.unindentDepth - 1);
else if (shouldUnindentLine && sw.unindentDepth > 0)
unindentLine(line, sw.unindentDepth);
}
/**
* convert a force-tab indent to spaces
*
* @param line a reference to the line that will be converted.
*/
void ASEnhancer::convertForceTabIndentToSpaces(string& line) const
{
// replace tab indents with spaces
for (size_t i = 0; i < line.length(); i++)
{
if (!isWhiteSpace(line[i]))
break;
if (line[i] == '\t')
{
line.erase(i, 1);
line.insert(i, tabLength, ' ');
i += tabLength - 1;
}
}
}
/**
* convert a space indent to force-tab
*
* @param line a reference to the line that will be converted.
*/
void ASEnhancer::convertSpaceIndentToForceTab(string& line) const
{
assert(tabLength > 0);
// replace leading spaces with tab indents
size_t newSpaceIndentLength = line.find_first_not_of(" \t");
size_t tabCount = newSpaceIndentLength / tabLength; // truncate extra spaces
line.replace(0U, tabCount * tabLength, tabCount, '\t');
}
/**
* find the colon following a 'case' statement
*
* @param line a reference to the line.
* @param caseIndex the line index of the case statement.
* @return the line index of the colon.
*/
size_t ASEnhancer::findCaseColon(const string& line, size_t caseIndex) const
{
size_t i = caseIndex;
bool isInQuote_ = false;
char quoteChar_ = ' ';
for (; i < line.length(); i++)
{
if (isInQuote_)
{
if (line[i] == '\\')
{
i++;
continue;
}
else if (line[i] == quoteChar_) // check ending quote
{
isInQuote_ = false;
quoteChar_ = ' ';
continue;
}
else
{
continue; // must close quote before continuing
}
}
if (line[i] == '"' // check opening quote
|| (line[i] == '\'' && !isDigitSeparator(line, i)))
{
isInQuote_ = true;
quoteChar_ = line[i];
continue;
}
if (line[i] == ':')
{
if ((i + 1 < line.length()) && (line[i + 1] == ':'))
i++; // bypass scope resolution operator
else
break; // found it
}
}
return i;
}
/**
* indent a line by a given number of tabsets
* by inserting leading whitespace to the line argument.
*
* @param line a reference to the line to indent.
* @param indent the number of tabsets to insert.
* @return the number of characters inserted.
*/
int ASEnhancer::indentLine(string& line, int indent) const
{
if (line.length() == 0
&& !emptyLineFill)
return 0;
size_t charsToInsert = 0;
if (forceTab && indentLength != tabLength)
{
// replace tab indents with spaces
convertForceTabIndentToSpaces(line);
// insert the space indents
charsToInsert = indent * indentLength;
line.insert(line.begin(), charsToInsert, ' ');
// replace leading spaces with tab indents
convertSpaceIndentToForceTab(line);
}
else if (useTabs)
{
charsToInsert = indent;
line.insert(line.begin(), charsToInsert, '\t');
}
else // spaces
{
charsToInsert = indent * indentLength;
line.insert(line.begin(), charsToInsert, ' ');
}
return charsToInsert;
}
/**
* check for SQL "BEGIN DECLARE SECTION".
* must compare case insensitive and allow any spacing between words.
*
* @param line a reference to the line to indent.
* @param index the current line index.
* @return true if a hit.
*/
bool ASEnhancer::isBeginDeclareSectionSQL(const string& line, size_t index) const
{
string word;
size_t hits = 0;
size_t i;
for (i = index; i < line.length(); i++)
{
i = line.find_first_not_of(" \t", i);
if (i == string::npos)
return false;
if (line[i] == ';')
break;
if (!isCharPotentialHeader(line, i))
continue;
word = getCurrentWord(line, i);
for (size_t j = 0; j < word.length(); j++)
word[j] = (char) toupper(word[j]);
if (word == "EXEC" || word == "SQL")
{
i += word.length() - 1;
continue;
}
if (word == "DECLARE" || word == "SECTION")
{
hits++;
i += word.length() - 1;
continue;
}
if (word == "BEGIN")
{
hits++;
i += word.length() - 1;
continue;
}
return false;
}
if (hits == 3)
return true;
return false;
}
/**
* check for SQL "END DECLARE SECTION".
* must compare case insensitive and allow any spacing between words.
*
* @param line a reference to the line to indent.
* @param index the current line index.
* @return true if a hit.
*/
bool ASEnhancer::isEndDeclareSectionSQL(const string& line, size_t index) const
{
string word;
size_t hits = 0;
size_t i;
for (i = index; i < line.length(); i++)
{
i = line.find_first_not_of(" \t", i);
if (i == string::npos)
return false;
if (line[i] == ';')
break;
if (!isCharPotentialHeader(line, i))
continue;
word = getCurrentWord(line, i);
for (size_t j = 0; j < word.length(); j++)
word[j] = (char) toupper(word[j]);
if (word == "EXEC" || word == "SQL")
{
i += word.length() - 1;
continue;
}
if (word == "DECLARE" || word == "SECTION")
{
hits++;
i += word.length() - 1;
continue;
}
if (word == "END")
{
hits++;
i += word.length() - 1;
continue;
}
return false;
}
if (hits == 3)
return true;
return false;
}
/**
* check if a one-line brace has been reached,
* i.e. if the currently reached '{' character is closed
* with a complimentary '}' elsewhere on the current line,
*.
* @return false = one-line brace has not been reached.
* true = one-line brace has been reached.
*/
bool ASEnhancer::isOneLineBlockReached(const string& line, int startChar) const
{
assert(line[startChar] == '{');
bool isInComment_ = false;
bool isInQuote_ = false;
int _braceCount = 1;
int lineLength = line.length();
char quoteChar_ = ' ';
char ch = ' ';
for (int i = startChar + 1; i < lineLength; ++i)
{
ch = line[i];
if (isInComment_)
{
if (line.compare(i, 2, "*/") == 0)
{
isInComment_ = false;
++i;
}
continue;
}
if (ch == '\\')
{
++i;
continue;
}
if (isInQuote_)
{
if (ch == quoteChar_)
isInQuote_ = false;
continue;
}
if (ch == '"'
|| (ch == '\'' && !isDigitSeparator(line, i)))
{
isInQuote_ = true;
quoteChar_ = ch;
continue;
}
if (line.compare(i, 2, "//") == 0)
break;
if (line.compare(i, 2, "/*") == 0)
{
isInComment_ = true;
++i;
continue;
}
if (ch == '{')
++_braceCount;
else if (ch == '}')
--_braceCount;
if (_braceCount == 0)
return true;
}
return false;
}
/**
* parse characters in the current line to determine if an indent
* or unindent is needed.
*/
void ASEnhancer::parseCurrentLine(string& line, bool isInPreprocessor, bool isInSQL)
{
bool isSpecialChar = false; // is a backslash escape character
for (size_t i = 0; i < line.length(); i++)
{
char ch = line[i];
// bypass whitespace
if (isWhiteSpace(ch))
continue;
// handle special characters (i.e. backslash+character such as \n, \t, ...)
if (isSpecialChar)
{
isSpecialChar = false;
continue;
}
if (!(isInComment) && line.compare(i, 2, "\\\\") == 0)
{
i++;
continue;
}
if (!(isInComment) && ch == '\\')
{
isSpecialChar = true;
continue;
}
// handle quotes (such as 'x' and "Hello Dolly")
if (!isInComment
&& (ch == '"'
|| (ch == '\'' && !isDigitSeparator(line, i))))
{
if (!isInQuote)
{
quoteChar = ch;
isInQuote = true;
}
else if (quoteChar == ch)
{
isInQuote = false;
continue;
}
}
if (isInQuote)
continue;
// handle comments
if (!(isInComment) && line.compare(i, 2, "//") == 0)
{
// check for windows line markers
if (line.compare(i + 2, 1, "\xf0") > 0)
lineNumber--;
// unindent if not in case braces
if (line.find_first_not_of(" \t") == i
&& sw.switchBraceCount == 1
&& sw.unindentCase)
shouldUnindentComment = true;
break; // finished with the line
}
else if (!(isInComment) && line.compare(i, 2, "/*") == 0)
{
// unindent if not in case braces
if (sw.switchBraceCount == 1 && sw.unindentCase)
shouldUnindentComment = true;
isInComment = true;
size_t commentEnd = line.find("*/", i);
if (commentEnd == string::npos)
i = line.length() - 1;
else
i = commentEnd - 1;
continue;
}
else if ((isInComment) && line.compare(i, 2, "*/") == 0)
{
// unindent if not in case braces
if (sw.switchBraceCount == 1 && sw.unindentCase)
shouldUnindentComment = true;
isInComment = false;
i++;
continue;
}
if (isInComment)
{
// unindent if not in case braces
if (sw.switchBraceCount == 1 && sw.unindentCase)
shouldUnindentComment = true;
size_t commentEnd = line.find("*/", i);
if (commentEnd == string::npos)
i = line.length() - 1;
else
i = commentEnd - 1;
continue;
}
// if we have reached this far then we are NOT in a comment or string of special characters
if (line[i] == '{')
braceCount++;
if (line[i] == '}')
braceCount--;
// check for preprocessor within an event table
if (isInEventTable && line[i] == '#' && preprocBlockIndent)
{
string preproc;
preproc = line.substr(i + 1);
if (preproc.substr(0, 2) == "if") // #if, #ifdef, #ifndef)
eventPreprocDepth += 1;
if (preproc.substr(0, 5) == "endif" && eventPreprocDepth > 0)
eventPreprocDepth -= 1;
}
bool isPotentialKeyword = isCharPotentialHeader(line, i);
// ---------------- wxWidgets and MFC macros ----------------------------------
if (isPotentialKeyword)
{
for (size_t j = 0; j < indentableMacros->size(); j++)
{
// 'first' is the beginning macro
if (findKeyword(line, i, indentableMacros->at(j)->first))
{
nextLineIsEventIndent = true;
break;
}
}
for (size_t j = 0; j < indentableMacros->size(); j++)
{
// 'second' is the ending macro
if (findKeyword(line, i, indentableMacros->at(j)->second))
{
isInEventTable = false;
eventPreprocDepth = 0;
break;
}
}
}
// ---------------- process SQL -----------------------------------------------
if (isInSQL)
{
if (isBeginDeclareSectionSQL(line, i))
nextLineIsDeclareIndent = true;
if (isEndDeclareSectionSQL(line, i))
isInDeclareSection = false;
break;
}
// ---------------- process switch statements ---------------------------------
if (isPotentialKeyword && findKeyword(line, i, ASResource::AS_SWITCH))
{
switchDepth++;
switchStack.emplace_back(sw); // save current variables
sw.switchBraceCount = 0;
sw.unindentCase = false; // don't clear case until end of switch
i += 5; // bypass switch statement
continue;
}
// just want unindented case statements from this point
if (caseIndent
|| switchDepth == 0
|| (isInPreprocessor && !preprocDefineIndent))
{
// bypass the entire word
if (isPotentialKeyword)
{
string name = getCurrentWord(line, i);
i += name.length() - 1;
}
continue;
}
i = processSwitchBlock(line, i);
} // end of for loop * end of for loop * end of for loop * end of for loop
}
/**
* process the character at the current index in a switch block.
*
* @param line a reference to the line to indent.
* @param index the current line index.
* @return the new line index.
*/
size_t ASEnhancer::processSwitchBlock(string& line, size_t index)
{
size_t i = index;
bool isPotentialKeyword = isCharPotentialHeader(line, i);
if (line[i] == '{')
{
sw.switchBraceCount++;
if (lookingForCaseBrace) // if 1st after case statement
{
sw.unindentCase = true; // unindenting this case
sw.unindentDepth++;
lookingForCaseBrace = false; // not looking now
}
return i;
}
lookingForCaseBrace = false; // no opening brace, don't indent
if (line[i] == '}')
{
sw.switchBraceCount--;
assert(sw.switchBraceCount <= braceCount);
if (sw.switchBraceCount == 0) // if end of switch statement
{
int lineUnindent = sw.unindentDepth;
if (line.find_first_not_of(" \t") == i
&& !switchStack.empty())
lineUnindent = switchStack[switchStack.size() - 1].unindentDepth;
if (shouldUnindentLine)
{
if (lineUnindent > 0)
i -= unindentLine(line, lineUnindent);
shouldUnindentLine = false;
}
switchDepth--;
sw = switchStack.back();
switchStack.pop_back();
}
return i;
}
if (isPotentialKeyword
&& (findKeyword(line, i, ASResource::AS_CASE)
|| findKeyword(line, i, ASResource::AS_DEFAULT)))
{
if (sw.unindentCase) // if unindented last case
{
sw.unindentCase = false; // stop unindenting previous case
sw.unindentDepth--;
}
i = findCaseColon(line, i);
i++;
for (; i < line.length(); i++) // bypass whitespace
{
if (!isWhiteSpace(line[i]))
break;
}
if (i < line.length())
{
if (line[i] == '{')
{
braceCount++;
sw.switchBraceCount++;
if (!isOneLineBlockReached(line, i))
unindentNextLine = true;
return i;
}
}
lookingForCaseBrace = true;
i--; // need to process this char
return i;
}
if (isPotentialKeyword)
{
string name = getCurrentWord(line, i); // bypass the entire name
i += name.length() - 1;
}
return i;
}
/**
* unindent a line by a given number of tabsets
* by erasing the leading whitespace from the line argument.
*
* @param line a reference to the line to unindent.
* @param unindent the number of tabsets to erase.
* @return the number of characters erased.
*/
int ASEnhancer::unindentLine(string& line, int unindent) const
{
size_t whitespace = line.find_first_not_of(" \t");
if (whitespace == string::npos) // if line is blank
whitespace = line.length(); // must remove padding, if any
if (whitespace == 0)
return 0;
size_t charsToErase = 0;
if (forceTab && indentLength != tabLength)
{
// replace tab indents with spaces
convertForceTabIndentToSpaces(line);
// remove the space indents
size_t spaceIndentLength = line.find_first_not_of(" \t");
charsToErase = unindent * indentLength;
if (charsToErase <= spaceIndentLength)
line.erase(0, charsToErase);
else
charsToErase = 0;
// replace leading spaces with tab indents
convertSpaceIndentToForceTab(line);
}
else if (useTabs)
{
charsToErase = unindent;
if (charsToErase <= whitespace)
line.erase(0, charsToErase);
else
charsToErase = 0;
}
else // spaces
{
charsToErase = unindent * indentLength;
if (charsToErase <= whitespace)
line.erase(0, charsToErase);
else
charsToErase = 0;
}
return charsToErase;
}
} // end namespace astyle

7714
thirdparty/astyle/ASFormatter.cpp vendored Executable file

File diff suppressed because it is too large Load Diff

1092
thirdparty/astyle/ASLocalizer.cpp vendored Executable file

File diff suppressed because it is too large Load Diff

159
thirdparty/astyle/ASLocalizer.h vendored Executable file
View File

@ -0,0 +1,159 @@
// ASLocalizer.h
// Copyright (c) 2017 by Jim Pattee <jimp03@email.com>.
// This code is licensed under the MIT License.
// License.md describes the conditions under which this software may be distributed.
#ifndef ASLOCALIZER_H
#define ASLOCALIZER_H
#include <string>
#include <vector>
namespace astyle {
using namespace std;
#ifndef ASTYLE_LIB
//-----------------------------------------------------------------------------
// ASLocalizer class for console build.
// This class encapsulates all language-dependent settings and is a
// generalization of the C locale concept.
//-----------------------------------------------------------------------------
class Translation;
class ASLocalizer
{
public: // functions
ASLocalizer();
virtual ~ASLocalizer();
string getLanguageID() const;
const Translation* getTranslationClass() const;
#ifdef _WIN32
void setLanguageFromLCID(size_t lcid);
#endif
void setLanguageFromName(const char* langID);
const char* settext(const char* textIn) const;
private: // functions
void setTranslationClass();
private: // variables
Translation* m_translation; // pointer to a polymorphic Translation class
string m_langID; // language identifier from the locale
string m_subLangID; // sub language identifier, if needed
string m_localeName; // name of the current locale (Linux only)
size_t m_lcid; // LCID of the user locale (Windows only)
};
//----------------------------------------------------------------------------
// Translation base class.
//----------------------------------------------------------------------------
class Translation
// This base class is inherited by the language translation classes.
// Polymorphism is used to call the correct language translator.
// This class contains the translation vector and settext translation method.
// The language vector is built by the language sub classes.
// NOTE: This class must have virtual methods for typeid() to work.
// typeid() is used by AStyleTestI18n_Localizer.cpp.
{
public:
Translation() {}
virtual ~Translation() {}
string convertToMultiByte(const wstring& wideStr) const;
size_t getTranslationVectorSize() const;
bool getWideTranslation(const string& stringIn, wstring& wideOut) const;
string& translate(const string& stringIn) const;
protected:
void addPair(const string& english, const wstring& translated);
// variables
vector<pair<string, wstring> > m_translation; // translation vector
private:
mutable string m_mbTranslation;
};
//----------------------------------------------------------------------------
// Translation classes
// One class for each language.
// These classes have only a constructor which builds the language vector.
//----------------------------------------------------------------------------
class Bulgarian : public Translation
{ public: Bulgarian(); };
class ChineseSimplified : public Translation
{ public: ChineseSimplified(); };
class ChineseTraditional : public Translation
{ public: ChineseTraditional(); };
class Dutch : public Translation
{ public: Dutch(); };
class English : public Translation
{ public: English(); };
class Estonian : public Translation
{ public: Estonian(); };
class Finnish : public Translation
{ public: Finnish(); };
class French : public Translation
{ public: French(); };
class German : public Translation
{ public: German(); };
class Greek : public Translation
{ public: Greek(); };
class Hindi : public Translation
{ public: Hindi(); };
class Hungarian : public Translation
{ public: Hungarian(); };
class Italian : public Translation
{ public: Italian(); };
class Japanese : public Translation
{ public: Japanese(); };
class Korean : public Translation
{ public: Korean(); };
class Norwegian : public Translation
{ public: Norwegian(); };
class Polish : public Translation
{ public: Polish(); };
class Portuguese : public Translation
{ public: Portuguese(); };
class Romanian : public Translation
{ public: Romanian(); };
class Russian : public Translation
{ public: Russian(); };
class Spanish : public Translation
{ public: Spanish(); };
class Swedish : public Translation
{ public: Swedish(); };
class Ukrainian : public Translation
{ public: Ukrainian(); };
#endif // ASTYLE_LIB
} // namespace astyle
#endif // ASLOCALIZER_H

833
thirdparty/astyle/ASResource.cpp vendored Executable file
View File

@ -0,0 +1,833 @@
// ASResource.cpp
// Copyright (c) 2017 by Jim Pattee <jimp03@email.com>.
// This code is licensed under the MIT License.
// License.md describes the conditions under which this software may be distributed.
//-----------------------------------------------------------------------------
// headers
//-----------------------------------------------------------------------------
#include "astyle.h"
#include <algorithm>
//-----------------------------------------------------------------------------
// astyle namespace
//-----------------------------------------------------------------------------
namespace astyle {
//
const string ASResource::_AS_EXCEPT = string("__except");
const string ASResource::_AS_FINALLY = string("__finally");
const string ASResource::_AS_TRY = string("__try");
const string ASResource::AS_ADD = string("add");
const string ASResource::AS_AUTO = string("auto");
const string ASResource::AS_AUTORELEASEPOOL = string("autoreleasepool");
const string ASResource::AS_CASE = string("case");
const string ASResource::AS_CATCH = string("catch");
const string ASResource::AS_CLASS = string("class");
const string ASResource::AS_CONST =