Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
)

add_clang_library(clangTidyRawSpeedModule
NoStdOptionalCheck.cpp
StdArrayNoOperatorAtCheck.cpp
RawSpeedTidyModule.cpp

Expand Down
49 changes: 49 additions & 0 deletions src/NoStdOptionalCheck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===--- NoStdOptionalCheck.cpp - clang-tidy ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "NoStdOptionalCheck.h"
#include <clang/AST/ASTContext.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>

using namespace clang;
using namespace clang::ast_matchers;

namespace {

AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
return Node.getBeginLoc().isValid();
}

} // namespace

namespace clang::tidy::rawspeed {

NoStdOptionalCheck::NoStdOptionalCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}

void NoStdOptionalCheck::registerMatchers(MatchFinder *Finder) {
auto StdOptionalClass = cxxRecordDecl(isSameOrDerivedFrom(
cxxRecordDecl(isInStdNamespace(), hasName("::std::optional"))));
auto Matcher = elaboratedTypeLoc(
hasValidBeginLoc(), loc(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(StdOptionalClass)))));
Finder->addMatcher(Matcher.bind("type"), this);
}

void NoStdOptionalCheck::check(const MatchFinder::MatchResult &Result) {
const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("type");
if (!TL)
return;

diag(TL->getBeginLoc(),
"Do not use 'std::optional' directly, use 'Optional' wrapper")
<< TL->getSourceRange();
}

} // namespace clang::tidy::rawspeed
27 changes: 27 additions & 0 deletions src/NoStdOptionalCheck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===--- NoStdOptionalCheck.h - clang-tidy -------------00000000-*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RAWSPEED_NOSTDOPTIONAL_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RAWSPEED_NOSTDOPTIONAL_H

#include <clang-tidy/ClangTidyCheck.h>

namespace clang::tidy::rawspeed {

/// Do not use `std::optional<>` directly, use `rawspeed::Optional<>`.
class NoStdOptionalCheck : public ClangTidyCheck {
public:
NoStdOptionalCheck(StringRef Name, ClangTidyContext *Context);
void registerMatchers(ast_matchers::MatchFinder *Finder) override;

void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};

} // namespace clang::tidy::rawspeed

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RAWSPEED_NOSTDOPTIONAL_H
3 changes: 3 additions & 0 deletions src/RawSpeedTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <clang-tidy/ClangTidyModule.h>
#include <clang-tidy/ClangTidyModuleRegistry.h>

#include "NoStdOptionalCheck.h"
#include "StdArrayNoOperatorAtCheck.h"

using namespace clang::ast_matchers;
Expand All @@ -21,6 +22,8 @@ namespace rawspeed {
class RawSpeedModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<NoStdOptionalCheck>(
"rawspeed-no-std-optional");
CheckFactories.registerCheck<StdArrayNoOperatorAtCheck>(
"rawspeed-std-array-no-operator-at");
}
Expand Down
222 changes: 222 additions & 0 deletions test/no-std-optional.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
// RUN: %check_clang_tidy -std=c++17-or-later %s rawspeed-no-std-optional %t

namespace std {

template <typename T> class optional {
public:
optional() = default;

template <typename U> optional(U);
};

template <typename T> class not_an_optional {
public:
not_an_optional() = default;

template <typename U> not_an_optional(U);
};

template <typename T> struct tuple {
T t;
};

}; // namespace std

namespace some_other_namespace {

template <typename T> class optional {
public:
optional() = default;

template <typename U> optional(U);
};

template <typename T> class not_an_optional {
public:
not_an_optional() = default;

template <typename U> not_an_optional(U);
};

template <typename T> struct tuple {
T t;
};

}; // namespace some_other_namespace

template <typename T> using my_optional_alias = std::optional<T>;

namespace {

template <typename T> class my_another_optional : public std::optional<T> {
// CHECK-MESSAGES: :[[@LINE-1]]:58: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
public:
my_another_optional() = default;

template <typename U> my_another_optional(U);
};

} // namespace

//------------------------------------------------------------------------------

std::optional<int> f0_return_type() {}
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
void f0_argument_type(std::optional<int>) {}
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
std::tuple<std::optional<int>> f0_other_return_type() {}
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}

struct S0 {
std::optional<int> f;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
std::optional<int> a, b;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
};

void exprrs_f0() {
std::optional<int>(1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
std::optional<int> a(1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
std::optional<int> b = 1;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}

f0_argument_type(1);
f0_argument_type(std::optional<int>(1));
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
auto c = f0_return_type();
auto [d] = f0_other_return_type();
}

//------------------------------------------------------------------------------

std::not_an_optional<int> f1_return_type() {}
void f1_argument_type(std::not_an_optional<int>) {}
std::tuple<std::not_an_optional<int>> f1_other_return_type() {}

struct S1 {
std::not_an_optional<int> f;
std::not_an_optional<int> a, b;
};

void exprrs_f1() {
std::not_an_optional<int>(1);
std::not_an_optional<int> a(1);
std::not_an_optional<int> b = 1;

f1_argument_type(1);
f1_argument_type(std::not_an_optional<int>(1));
auto c = f1_return_type();
auto [d] = f1_other_return_type();
}

//------------------------------------------------------------------------------

some_other_namespace::optional<int> f2_return_type() {}
void f2_argument_type(some_other_namespace::optional<int>) {}
some_other_namespace::tuple<some_other_namespace::optional<int>>
f2_other_return_type() {}

struct S2 {
some_other_namespace::optional<int> f;
some_other_namespace::optional<int> a, b;
};

void exprrs_f2() {
some_other_namespace::optional<int>(1);
some_other_namespace::optional<int> a(1);
some_other_namespace::optional<int> b = 1;

f2_argument_type(1);
f2_argument_type(some_other_namespace::optional<int>(1));
auto c = f2_return_type();
auto [d] = f2_other_return_type();
}

//------------------------------------------------------------------------------

some_other_namespace::not_an_optional<int> f3_return_type() {}
void f3_argument_type(some_other_namespace::not_an_optional<int>) {}
some_other_namespace::tuple<some_other_namespace::not_an_optional<int>>
f3_other_return_type() {}

struct S3 {
some_other_namespace::not_an_optional<int> f;
some_other_namespace::not_an_optional<int> a, b;
};

void exprrs_f3() {
some_other_namespace::not_an_optional<int>(1);
some_other_namespace::not_an_optional<int> a(1);
some_other_namespace::not_an_optional<int> b = 1;

f3_argument_type(1);
f3_argument_type(some_other_namespace::not_an_optional<int>(1));
auto c = f3_return_type();
auto [d] = f3_other_return_type();
}

//------------------------------------------------------------------------------

my_optional_alias<int> f4_return_type() {}
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
void f4_argument_type(my_optional_alias<int>) {}
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
std::tuple<my_optional_alias<int>> f4_other_return_type() {}
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}

struct S4 {
my_optional_alias<int> f;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
my_optional_alias<int> a, b;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
};

void exprrs_f4() {
my_optional_alias<int>(1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
my_optional_alias<int> a(1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
my_optional_alias<int> b = 1;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}

f4_argument_type(1);
f4_argument_type(my_optional_alias<int>(1));
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
auto c = f4_return_type();
auto [d] = f4_other_return_type();
}

//------------------------------------------------------------------------------

my_another_optional<int> f5_return_type() {}
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
void f5_argument_type(my_another_optional<int>) {}
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
std::tuple<my_another_optional<int>> f5_other_return_type() {}
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}

struct S5 {
my_another_optional<int> f;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
my_another_optional<int> a, b;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
};

void exprrs_f5() {
my_another_optional<int>(1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
my_another_optional<int> a(1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
my_another_optional<int> b = 1;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}

f5_argument_type(1);
f5_argument_type(my_another_optional<int>(1));
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: Do not use 'std::optional' directly, use 'Optional' wrapper [rawspeed-no-std-optional]{{$}}
auto c = f5_return_type();
auto [d] = f5_other_return_type();
}

//------------------------------------------------------------------------------