Skip to content

Commit

Permalink
test: allow standard includes in clang-tidy tests
Browse files Browse the repository at this point in the history
ported from: CleverRaven/Cataclysm-DDA#65806

Co-authored-by: Co-authored-by: John Bytheway <[email protected]>
  • Loading branch information
scarf005 and jbytheway committed Jul 22, 2023
1 parent 72c24fb commit 73e2f95
Show file tree
Hide file tree
Showing 26 changed files with 92 additions and 515 deletions.
26 changes: 2 additions & 24 deletions src/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#ifndef CATA_SRC_POINT_H
#define CATA_SRC_POINT_H

// The CATA_NO_STL macro is used by the cata clang-tidy plugin tests so they
// can include this header when compiling with -nostdinc++
#ifndef CATA_NO_STL

#include <array>
#include <cassert>
#include <climits>
Expand All @@ -16,18 +12,6 @@
#include <string>
#include <vector>

#else

#define assert(...)

namespace std
{
class string;
class ostream;
}

#endif // CATA_NO_STL

class JsonIn;
class JsonOut;

Expand Down Expand Up @@ -74,11 +58,9 @@ struct point {
return point( x / rhs, y / rhs );
}

#ifndef CATA_NO_STL
inline point abs() const {
return point( std::abs( x ), std::abs( y ) );
}
#endif

/**
* Rotate point clockwise @param turns times, 90 degrees per turn,
Expand All @@ -104,6 +86,7 @@ struct point {
}

friend std::ostream &operator<<( std::ostream &, point );
friend std::istream &operator>>( std::istream &, point );
};

inline int divide_round_to_minus_infinity( int n, int d )
Expand Down Expand Up @@ -197,11 +180,9 @@ struct tripoint {
return *this;
}

#ifndef CATA_NO_STL
inline tripoint abs() const {
return tripoint( std::abs( x ), std::abs( y ), std::abs( z ) );
}
#endif

constexpr point xy() const {
return point( x, y );
Expand All @@ -223,6 +204,7 @@ struct tripoint {
void deserialize( JsonIn &jsin );

friend std::ostream &operator<<( std::ostream &, const tripoint & );
friend std::istream &operator>>( std::istream &, const tripoint & );

friend inline constexpr bool operator==( const tripoint &a, const tripoint &b ) {
return a.x == b.x && a.y == b.y && a.z == b.z;
Expand Down Expand Up @@ -289,8 +271,6 @@ struct sphere {
explicit sphere( const tripoint &center, int radius ) : radius( radius ), center( center ) {}
};

#ifndef CATA_NO_STL

/**
* Following functions return points in a spiral pattern starting at center_x/center_y until it hits the radius. Clockwise fashion.
* Credit to Tom J Nowell; http://stackoverflow.com/a/1555236/1269969
Expand Down Expand Up @@ -388,6 +368,4 @@ static const std::array<tripoint, 8> eight_horizontal_neighbors = { {
}
};

#endif // CATA_NO_STL

#endif // CATA_SRC_POINT_H
29 changes: 20 additions & 9 deletions tools/clang-tidy-plugin/JsonTranslationInputCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,26 @@ void JsonTranslationInputCheck::registerMatchers( MatchFinder *Finder )
) ),
// <translation function>( ... )
hasAncestor(
callExpr( callee( decl( anyOf(
functionDecl(
hasAnyName( "_", "translation_argument_identity", "gettext", "pgettext", "n_gettext", "npgettext" )
).bind( "translationFunc" ),
functionDecl(
hasAnyName( "to_translation", "pl_translation" )
) ) ) )
// no_translation is ok, it's used to load generated names such as artifact names
).bind( "translationCall" ) )
callExpr(
callee(
decl(
anyOf(
functionDecl(
hasAnyName(
"_", "translation_argument_identity", "gettext", "pgettext",
"n_gettext", "npgettext"
)
).bind( "translationFunc" ),
functionDecl(
hasAnyName( "to_translation", "pl_translation" )
)
// no_translation is ok, it's used to load generated names such as
// artifact names
)
)
)
).bind( "translationCall" )
)
).bind( "jsonInputCall" ),
this
);
Expand Down
4 changes: 3 additions & 1 deletion tools/clang-tidy-plugin/test/check_clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def __init__(self, args, extra_args):

# Tests should not rely on STL being available, and instead provide mock
# implementations of relevant APIs.
self.clang_extra_args.append('-nostdinc++')
if not args.allow_stdinc:
self.clang_extra_args.append('-nostdinc++')

if self.resource_dir is not None:
self.clang_extra_args.append('-resource-dir=%s' % self.resource_dir)
Expand Down Expand Up @@ -249,6 +250,7 @@ def parse_arguments():
type=csv,
help='comma-separated list of FileCheck suffixes')
parser.add_argument('-std', type=csv, default=['c++11-or-later'])
parser.add_argument('-allow-stdinc', action='store_true')
return parser.parse_known_args()


Expand Down
3 changes: 1 addition & 2 deletions tools/clang-tidy-plugin/test/combine-locals-into-point.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// RUN: %check_clang_tidy %s cata-combine-locals-into-point %t -- --load=%cata_plugin -- -isystem %cata_include
// RUN: %check_clang_tidy -allow-stdinc %s cata-combine-locals-into-point %t -- --load=%cata_plugin -- -isystem %cata_include

#define CATA_NO_STL
#include "point.h"

void f0( const point & );
Expand Down
55 changes: 5 additions & 50 deletions tools/clang-tidy-plugin/test/determinism.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,8 @@
// RUN: %check_clang_tidy %s cata-determinism %t -- --load=%cata_plugin --
// RUN: %check_clang_tidy -allow-stdinc %s cata-determinism %t -- --load=%cata_plugin -- -isystem %cata_include

using size_t = unsigned long;
#include <random>

namespace std
{

using uint_fast32_t = unsigned;

template <

class UIntType,
UIntType a,
UIntType c,
UIntType m
> struct linear_congruential_engine
{
using result_type = UIntType;
explicit linear_congruential_engine( result_type value );
void seed( result_type value );
};
using minstd_rand0 = std::linear_congruential_engine<std::uint_fast32_t, 16807, 0, 2147483647>;

template <

class UIntType,
size_t w, size_t n, size_t m, size_t r,
UIntType a, size_t u, UIntType d, size_t s,
UIntType b, size_t t,
UIntType c, size_t l, UIntType f
> struct mersenne_twister_engine
{
using result_type = UIntType;
void seed( result_type value );
};
using mt19937 = std::mersenne_twister_engine<std::uint_fast32_t, 32, 624, 397, 31,
0x9908b0df, 11,
0xffffffff, 7,
0x9d2c5680, 15,
0xefc60000, 18, 1812433253>;
}
#include "rng.h"

struct SomeOtherStruct {
int i;
Expand All @@ -51,15 +15,6 @@ struct StructWithSeedButNotResultType {

using SomeOtherAlias = SomeOtherStruct;

int rand();

using cata_default_random_engine = std::minstd_rand0;

namespace std
{
using ::rand;
}

namespace other
{
int rand();
Expand All @@ -68,9 +23,9 @@ int rand();
void f()
{
std::mt19937 gen0;
// CHECK-MESSAGES: [[@LINE-1]]:18: warning: Construction of library random engine 'std::mt19937' (aka 'mersenne_twister_engine<unsigned int, 32, 624, 397, 31, 2567483615U, 11, 4294967295U, 7, 2636928640U, 15, 4022730752U, 18, 1812433253>'). To ensure determinism for a fixed seed, use the common tools from rng.h rather than your own random number engines. [cata-determinism]
// CHECK-MESSAGES: [[@LINE-1]]:18: warning: Construction of library random engine 'std::mt19937' (aka 'mersenne_twister_engine<unsigned long, 32, 624, 397, 31, 2567483615UL, 11, 4294967295UL, 7, 2636928640UL, 15, 4022730752UL, 18, 1812433253UL>'). To ensure determinism for a fixed seed, use the common tools from rng.h rather than your own random number engines. [cata-determinism]
cata_default_random_engine gen1( 0 );
// CHECK-MESSAGES: [[@LINE-1]]:32: warning: Construction of library random engine 'cata_default_random_engine' (aka 'linear_congruential_engine<unsigned int, 16807, 0, 2147483647>'). To ensure determinism for a fixed seed, use the common tools from rng.h rather than your own random number engines. [cata-determinism]
// CHECK-MESSAGES: [[@LINE-1]]:32: warning: Construction of library random engine 'cata_default_random_engine' (aka 'linear_congruential_engine<unsigned long, 16807UL, 0UL, 2147483647UL>'). To ensure determinism for a fixed seed, use the common tools from rng.h rather than your own random number engines. [cata-determinism]
SomeOtherStruct s0;
StructWithSeedButNotResultType s1;
SomeOtherAlias a;
Expand Down
69 changes: 5 additions & 64 deletions tools/clang-tidy-plugin/test/json-translation-input.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,7 @@
// RUN: %check_clang_tidy %s cata-json-translation-input %t -- --load=%cata_plugin -- -I %test_include
// RUN: %check_clang_tidy -allow-stdinc %s cata-json-translation-input %t -- --load=%cata_plugin -- -isystem %cata_include -isystem %cata_include/third-party -DLOCALIZE

// check_clang_tidy uses -nostdinc++, so we add dummy declaration of std::string here
namespace std
{
template<class CharT, class Traits = void, class Allocator = void>
class basic_string
{
public:
basic_string();
basic_string( const CharT * );
CharT *c_str();
const CharT *c_str() const;
};
using string = basic_string<char>;
string operator+( const string &, const string & );
} // namespace std

// check_clang_tidy uses -nostdinc++, so we add dummy translation interface here instead of including translations.h
std::string _( const std::string & );
std::string gettext( const std::string & );
std::string pgettext( const std::string &, const std::string & );
std::string vgettext( const std::string &, const std::string &, int );
std::string vpgettext( const std::string &, const std::string &, const std::string &, int );

class translation
{
public:
static translation to_translation( const std::string & );
static translation to_translation( const std::string &, const std::string & );
static translation pl_translation( const std::string &, const std::string & );
static translation pl_translation( const std::string &, const std::string &, const std::string & );
static translation no_translation( const std::string & );
};

translation to_translation( const std::string & );
translation to_translation( const std::string &, const std::string & );
translation pl_translation( const std::string &, const std::string & );
translation pl_translation( const std::string &, const std::string &, const std::string & );
translation no_translation( const std::string & );

// dummy json interface
class JsonArray
{
public:
std::string next_string();
};

class JsonObject
{
public:
std::string get_string( const std::string & );
JsonArray get_array( const std::string & );
template<class T>
bool read( const std::string &name, T &t, bool throw_on_error = true );
};

class JsonIn
{
public:
JsonObject get_object();
};
#include "json.h"
#include "translations.h"

class foo
{
Expand All @@ -76,9 +18,8 @@ class foo
};

// <translation function>( ... <json input object>.<method>(...) ... )
static void deserialize( foo &bar, JsonIn &jin )
static void deserialize( foo &bar, JsonObject &jo )
{
JsonObject jo = jin.get_object();
bar.name = _( jo.get_string( "name" ) );
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: immediately translating a value read from json causes translation updating issues. Consider reading into a translation object instead.
// CHECK-MESSAGES: [[@LINE-2]]:19: note: value read from json
Expand All @@ -88,7 +29,7 @@ static void deserialize( foo &bar, JsonIn &jin )
// CHECK-MESSAGES: [[@LINE-1]]:36: warning: immediately translating a value read from json causes translation updating issues. Consider reading into a translation object instead.
// CHECK-MESSAGES: [[@LINE-2]]:39: note: value read from json
// CHECK-MESSAGES: [[@LINE-3]]:62: warning: immediately translating a value read from json causes translation updating issues. Consider reading into a translation object instead.
std::string( ja.next_string() ) );
std::string( ja.next_string() ).c_str() );
// CHECK-MESSAGES: [[@LINE-1]]:36: note: value read from json

// ok, not reading from json
Expand Down
9 changes: 2 additions & 7 deletions tools/clang-tidy-plugin/test/no-long.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// RUN: %check_clang_tidy %s cata-no-long %t -- --load=%cata_plugin --

#include <stdint.h>

// Want these defines without including limits.h. They're probably not the
// correct values, but it doesn't matter.
#define LONG_MIN -2147483647
#define LONG_MAX 2147483647
#define ULONG_MAX 4294967295
#include <inttypes.h>
#include <limits.h>

long i1;
// CHECK-MESSAGES: warning: Variable 'i1' declared as 'long'. Prefer int or int64_t to long. [cata-no-long]
Expand Down
4 changes: 2 additions & 2 deletions tools/clang-tidy-plugin/test/no-static-translation.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %check_clang_tidy %s cata-no-static-translation %t -- --load=%cata_plugin -- -I %test_include
// RUN: %check_clang_tidy -allow-stdinc %s cata-no-static-translation %t -- --load=%cata_plugin -- -I %cata_include -DLOCALIZE

#include "mock-translation.h"
#include "translations.h"

class foo
{
Expand Down
17 changes: 4 additions & 13 deletions tools/clang-tidy-plugin/test/ot-match.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
// RUN: %check_clang_tidy %s cata-ot-match %t -- --load=%cata_plugin -- -isystem %cata_include
// RUN: %check_clang_tidy -allow-stdinc %s cata-ot-match %t -- --load=%cata_plugin -- -isystem %cata_include -isystem %cata_include/third-party

struct oter_id;
#include "enums.h"
#include "overmap.h"
#include "type_id.h"

const oter_id &ter();

enum class ot_match_type : int {
exact,
type,
prefix,
contains,
num_ot_match_type
};

bool is_ot_match( const char *name, const oter_id &oter,
const ot_match_type match_type );

void f0()
{
is_ot_match( "ter0", ter(), ot_match_type::exact );
Expand Down
3 changes: 1 addition & 2 deletions tools/clang-tidy-plugin/test/point-initialization.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// RUN: %check_clang_tidy %s cata-point-initialization %t -- --load=%cata_plugin -- -isystem %cata_include
// RUN: %check_clang_tidy -allow-stdinc %s cata-point-initialization %t -- --load=%cata_plugin -- -isystem %cata_include

#define CATA_NO_STL
#include "point.h"

struct non_point {
Expand Down
Loading

0 comments on commit 73e2f95

Please sign in to comment.