clang-tools 24.0.0git
UseToUnderlyingCheck.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USETOUNDERLYINGCHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USETOUNDERLYINGCHECK_H
11
12#include "../ClangTidyCheck.h"
14#include <optional>
15
16namespace clang::tidy::modernize {
17
18/// Finds casts from a scoped enumeration to its underlying integer type and
19/// replaces them with a call to ``std::to_underlying`` (C++23).
20///
21/// For the user-facing documentation see:
22/// https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-to-underlying.html
24public:
25 /// How to treat an imprecise cast, i.e. a cast whose destination type is an
26 /// integer type other than the enumeration's underlying type.
27 enum class ImpreciseCastsKind {
28 /// Do not diagnose imprecise casts.
30 /// Diagnose imprecise casts but do not offer a fix-it.
32 /// Wrap the operand in a call to the replacement function, preserving the
33 /// original destination type.
35 /// Replace the whole cast with a call to the replacement function, changing
36 /// the resulting type to the underlying type.
38 };
39
40 UseToUnderlyingCheck(StringRef Name, ClangTidyContext *Context);
41 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
42 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
43 Preprocessor *ModuleExpanderPP) override;
44 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
45 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
46 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
47 std::optional<TraversalKind> getCheckTraversalKind() const override {
48 return TK_IgnoreUnlessSpelledInSource;
49 }
50
51private:
52 const ImpreciseCastsKind ImpreciseCasts;
53 const StringRef ReplacementFunction;
54 StringRef ReplacementFunctionHeader;
55 utils::IncludeInserter IncludeInserter;
56};
57
58} // namespace clang::tidy::modernize
59
60#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USETOUNDERLYINGCHECK_H
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
std::optional< TraversalKind > getCheckTraversalKind() const override
UseToUnderlyingCheck(StringRef Name, ClangTidyContext *Context)
ImpreciseCastsKind
How to treat an imprecise cast, i.e.
@ PreserveType
Wrap the operand in a call to the replacement function, preserving the original destination type.
@ Warn
Diagnose imprecise casts but do not offer a fix-it.
@ UseUnderlyingType
Replace the whole cast with a call to the replacement function, changing the resulting type to the un...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
llvm::StringMap< ClangTidyValue > OptionMap