reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
#include "ClangTidyTest.h"
#include "google/ExplicitConstructorCheck.h"
#include "google/GlobalNamesInHeadersCheck.h"
#include "gtest/gtest.h"

using namespace clang::tidy::google;

namespace clang {
namespace tidy {
namespace test {

TEST(ExplicitConstructorCheckTest, SingleArgumentConstructorsOnly) {
  EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(); };");
  EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(int i, int j); };");
  EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(const C&); };");
  EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(C&&); };");
  EXPECT_NO_CHANGES(ExplicitConstructorCheck,
                    "class C { C(const C&) = delete; };");
  EXPECT_NO_CHANGES(ExplicitConstructorCheck,
                    "class C { C(int) = delete; };");
}

TEST(ExplicitConstructorCheckTest, Basic) {
  EXPECT_EQ("class C { explicit C(int i); };",
            runCheckOnCode<ExplicitConstructorCheck>("class C { C(int i); };"));
}

TEST(ExplicitConstructorCheckTest, DefaultParameters) {
  EXPECT_EQ("class C { explicit C(int i, int j = 0); };",
            runCheckOnCode<ExplicitConstructorCheck>(
                "class C { C(int i, int j = 0); };"));
}

TEST(ExplicitConstructorCheckTest, OutOfLineDefinitions) {
  EXPECT_EQ("class C { explicit C(int i); }; C::C(int i) {}",
            runCheckOnCode<ExplicitConstructorCheck>(
                "class C { C(int i); }; C::C(int i) {}"));
}

TEST(ExplicitConstructorCheckTest, RemoveExplicit) {
  EXPECT_EQ("class A { A(const A&); };\n"
            "class B { /*asdf*/  B(B&&); };\n"
            "class C { /*asdf*/  C(const C&, int i = 0); };",
            runCheckOnCode<ExplicitConstructorCheck>(
                "class A { explicit    A(const A&); };\n"
                "class B { explicit   /*asdf*/  B(B&&); };\n"
                "class C { explicit/*asdf*/  C(const C&, int i = 0); };"));
}

TEST(ExplicitConstructorCheckTest, RemoveExplicitWithMacros) {
  EXPECT_EQ(
      "#define A(T) class T##Bar { explicit T##Bar(const T##Bar &b) {} };\n"
      "A(Foo);",
      runCheckOnCode<ExplicitConstructorCheck>(
          "#define A(T) class T##Bar { explicit T##Bar(const T##Bar &b) {} };\n"
          "A(Foo);"));
}

class GlobalNamesInHeadersCheckTest : public ::testing::Test {
protected:
  bool runCheckOnCode(const std::string &Code, const std::string &Filename) {
    static const char *const Header = "namespace std {\n"
                                      "class string {};\n"
                                      "}  // namespace std\n"
                                      "\n"
                                      "#define SOME_MACRO(x) using x\n";
    std::vector<ClangTidyError> Errors;
    std::vector<std::string> Args;
    if (!StringRef(Filename).endswith(".cpp")) {
      Args.emplace_back("-xc++-header");
    }
    test::runCheckOnCode<readability::GlobalNamesInHeadersCheck>(
        Header + Code, &Errors, Filename, Args);
    if (Errors.empty())
      return false;
    assert(Errors.size() == 1);
    assert(
        Errors[0].Message.Message ==
        "using declarations in the global namespace in headers are prohibited");
    return true;
  }
};

TEST_F(GlobalNamesInHeadersCheckTest, UsingDeclarations) {
  EXPECT_TRUE(runCheckOnCode("using std::string;", "foo.h"));
  EXPECT_FALSE(runCheckOnCode("using std::string;", "foo.cpp"));
  EXPECT_FALSE(runCheckOnCode("namespace my_namespace {\n"
                              "using std::string;\n"
                              "}  // my_namespace\n",
                              "foo.h"));
  EXPECT_FALSE(runCheckOnCode("SOME_MACRO(std::string);", "foo.h"));
}

TEST_F(GlobalNamesInHeadersCheckTest, UsingDirectives) {
  EXPECT_TRUE(runCheckOnCode("using namespace std;", "foo.h"));
  EXPECT_FALSE(runCheckOnCode("using namespace std;", "foo.cpp"));
  EXPECT_FALSE(runCheckOnCode("namespace my_namespace {\n"
                              "using namespace std;\n"
                              "}  // my_namespace\n",
                              "foo.h"));
  EXPECT_FALSE(runCheckOnCode("SOME_MACRO(namespace std);", "foo.h"));
}

TEST_F(GlobalNamesInHeadersCheckTest, RegressionAnonymousNamespace) {
  EXPECT_FALSE(runCheckOnCode("namespace {}", "foo.h"));
}

} // namespace test
} // namespace tidy
} // namespace clang