Skip to content

Commit

Permalink
Check text style in c++ code using clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrox committed Oct 6, 2019
1 parent 1acb13e commit 17c5df7
Show file tree
Hide file tree
Showing 8 changed files with 634 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ misc-*,\
modernize-*,\
performance-*,\
readability-*,\
-cata-text-style,\
-bugprone-misplaced-widening-cast,\
-bugprone-narrowing-conversions,\
-bugprone-unused-return-value,\
Expand Down Expand Up @@ -61,5 +62,7 @@ FormatStyle: none
CheckOptions:
- key: readability-uppercase-literal-suffix.NewSuffixes
value: 'L;UL;LL;ULL'
- key: cata-text-style.EscapeUnicode
value: 0

# vim:tw=0
2 changes: 2 additions & 0 deletions tools/clang-tidy-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ add_library(
NoStaticGettextCheck.cpp
PointInitializationCheck.cpp
SimplifyPointConstructorsCheck.cpp
StringLiteralIterator.cpp
TextStyleCheck.cpp
TranslatorCommentsCheck.cpp
UseNamedPointConstantsCheck.cpp
UsePointApisCheck.cpp
Expand Down
2 changes: 2 additions & 0 deletions tools/clang-tidy-plugin/CataTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "NoStaticGettextCheck.h"
#include "PointInitializationCheck.h"
#include "SimplifyPointConstructorsCheck.h"
#include "TextStyleCheck.h"
#include "TranslatorCommentsCheck.h"
#include "UseNamedPointConstantsCheck.h"
#include "UsePointApisCheck.h"
Expand All @@ -30,6 +31,7 @@ class CataModule : public ClangTidyModule
CheckFactories.registerCheck<PointInitializationCheck>( "cata-point-initialization" );
CheckFactories.registerCheck<SimplifyPointConstructorsCheck>(
"cata-simplify-point-constructors" );
CheckFactories.registerCheck<TextStyleCheck>( "cata-text-style" );
CheckFactories.registerCheck<TranslatorCommentsCheck>( "cata-translator-comments" );
CheckFactories.registerCheck<UseNamedPointConstantsCheck>(
"cata-use-named-point-constants" );
Expand Down
180 changes: 180 additions & 0 deletions tools/clang-tidy-plugin/StringLiteralIterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#include "StringLiteralIterator.h"

namespace clang
{
namespace tidy
{
namespace cata
{

StringLiteralIterator::StringLiteralIterator( const StringLiteral &str, size_t ind )
: str( str ), ind( ind )
{
}

SourceLocation StringLiteralIterator::toSourceLocation( const SourceManager &SrcMgr,
const LangOptions &LangOpts, const TargetInfo &Info ) const
{
return str.get().getLocationOfByte( ind, SrcMgr, LangOpts, Info );
}

uint32_t StringLiteralIterator::operator*() const
{
uint32_t ch = str.get().getCodeUnit( ind );
unsigned int n;
if( ch >= 0xFC ) {
ch &= 0x01;
n = 5;
} else if( ch >= 0xF8 ) {
ch &= 0x03;
n = 4;
} else if( ch >= 0xF0 ) {
ch &= 0x07;
n = 3;
} else if( ch >= 0xE0 ) {
ch &= 0x0F;
n = 2;
} else if( ch >= 0xC0 ) {
ch &= 0x1F;
n = 1;
} else {
n = 0;
}
for( size_t i = 1; i <= n; ++i ) {
ch = ( ch << 6 ) | ( str.get().getCodeUnit( ind + i ) & 0x3F );
}
return ch;
}

bool StringLiteralIterator::operator<( const StringLiteralIterator &rhs ) const
{
return ind < rhs.ind;
}

bool StringLiteralIterator::operator>( const StringLiteralIterator &rhs ) const
{
return rhs.operator < ( *this );
}

bool StringLiteralIterator::operator<=( const StringLiteralIterator &rhs ) const
{
return !rhs.operator < ( *this );
}

bool StringLiteralIterator::operator>=( const StringLiteralIterator &rhs ) const
{
return !operator<( rhs );
}

bool StringLiteralIterator::operator==( const StringLiteralIterator &rhs ) const
{
return ind == rhs.ind;
}

bool StringLiteralIterator::operator!=( const StringLiteralIterator &rhs ) const
{
return !operator==( rhs );
}

StringLiteralIterator &StringLiteralIterator::operator+=( ptrdiff_t inc )
{
if( inc == 0 ) {
return *this;
} else if( inc > 0 ) {
while( inc > 0 && ind < str.get().getLength() ) {
uint32_t byte = str.get().getCodeUnit( ind );
if( byte >= 0xFC ) {
ind += 6;
} else if( byte >= 0xF8 ) {
ind += 5;
} else if( byte >= 0xF0 ) {
ind += 4;
} else if( byte >= 0xE0 ) {
ind += 3;
} else if( byte >= 0xC0 ) {
ind += 2;
} else {
++ind;
}
--inc;
}
ind = std::min<size_t>( ind, str.get().getLength() );
} else {
while( inc < 0 && ind > 0 ) {
uint32_t byte = str.get().getCodeUnit( ind - 1 );
if( byte < 0x80 || byte >= 0xC0 ) {
++inc;
}
--ind;
}
}
return *this;
}

StringLiteralIterator &StringLiteralIterator::operator-=( ptrdiff_t dec )
{
return operator+=( -dec );
}

StringLiteralIterator StringLiteralIterator::operator+( ptrdiff_t inc ) const
{
StringLiteralIterator ret = *this;
ret.operator += ( inc );
return ret;
}

StringLiteralIterator StringLiteralIterator::operator-( ptrdiff_t dec ) const
{
return operator+( -dec );
}

StringLiteralIterator &StringLiteralIterator::operator++()
{
return operator+=( 1 );
}

StringLiteralIterator StringLiteralIterator::operator++( int )
{
StringLiteralIterator ret = *this;
operator++();
return ret;
}

StringLiteralIterator &StringLiteralIterator::operator--()
{
return operator-=( 1 );
}

StringLiteralIterator StringLiteralIterator::operator--( int )
{
StringLiteralIterator ret = *this;
operator--();
return ret;
}

StringLiteralIterator StringLiteralIterator::begin( const StringLiteral &str )
{
return StringLiteralIterator( str, 0 );
}

StringLiteralIterator StringLiteralIterator::end( const StringLiteral &str )
{
return StringLiteralIterator( str, str.getLength() );
}

ptrdiff_t StringLiteralIterator::distance( const StringLiteralIterator &beg,
const StringLiteralIterator &end )
{
if( beg <= end ) {
ptrdiff_t dist = 0;
for( auto it = beg; it < end; ++it, ++dist ) {
}
return dist;
} else {
return -distance( end, beg );
}
}

} // namespace cata
} // namespace tidy
} // namespace clang
59 changes: 59 additions & 0 deletions tools/clang-tidy-plugin/StringLiteralIterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef CATA_TOOLS_CLANG_TIDY_STRINGLITERALVIEW_H
#define CATA_TOOLS_CLANG_TIDY_STRINGLITERALVIEW_H

#include "ClangTidy.h"

namespace clang
{
namespace tidy
{
namespace cata
{

// Currently only supports utf8 becasue StringLiteral only has full support
// for single-byte ascii/utf8 strings
class StringLiteralIterator
{
public:
// This assumes that ind points to the start of a valid utf8 sequence
StringLiteralIterator( const StringLiteral &str, size_t ind );

SourceLocation toSourceLocation( const SourceManager &SrcMgr, const LangOptions &LangOpts,
const TargetInfo &Info ) const;

// All following operators assume that StringLiteral contains a valid
// utf8 string
uint32_t operator*() const;

bool operator<( const StringLiteralIterator &rhs ) const;
bool operator>( const StringLiteralIterator &rhs ) const;
bool operator<=( const StringLiteralIterator &rhs ) const;
bool operator>=( const StringLiteralIterator &rhs ) const;
bool operator==( const StringLiteralIterator &rhs ) const;
bool operator!=( const StringLiteralIterator &rhs ) const;

StringLiteralIterator &operator+=( ptrdiff_t inc );
StringLiteralIterator &operator-=( ptrdiff_t dec );
StringLiteralIterator operator+( ptrdiff_t inc ) const;
StringLiteralIterator operator-( ptrdiff_t dec ) const;
StringLiteralIterator &operator++();
StringLiteralIterator operator++( int );
StringLiteralIterator &operator--();
StringLiteralIterator operator--( int );

static StringLiteralIterator begin( const StringLiteral &str );
static StringLiteralIterator end( const StringLiteral &str );

static ptrdiff_t distance( const StringLiteralIterator &beg,
const StringLiteralIterator &end );

private:
std::reference_wrapper<const StringLiteral> str;
size_t ind;
};

} // namespace cata
} // namespace tidy
} // namespace clang

#endif // CATA_TOOLS_CLANG_TIDY_STRINGLITERALVIEW_H
Loading

0 comments on commit 17c5df7

Please sign in to comment.