2015-08-19 19:15:36 +08:00
|
|
|
//===--- IdentifierNamingCheck.h - clang-tidy -------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2015-08-19 19:15:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
|
|
|
|
|
2020-01-17 05:29:19 +08:00
|
|
|
#include "../utils/RenamerClangTidyCheck.h"
|
2020-08-01 17:35:13 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2015-08-19 19:15:36 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace readability {
|
|
|
|
|
2021-08-02 06:20:12 +08:00
|
|
|
enum StyleKind : int;
|
|
|
|
|
2015-08-28 02:01:58 +08:00
|
|
|
/// Checks for identifiers naming style mismatch.
|
|
|
|
///
|
|
|
|
/// This check will try to enforce coding guidelines on the identifiers naming.
|
|
|
|
/// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
|
|
|
|
/// and tries to convert from one to another if a mismatch is detected.
|
|
|
|
///
|
|
|
|
/// It also supports a fixed prefix and suffix that will be prepended or
|
|
|
|
/// appended to the identifiers, regardless of the casing.
|
|
|
|
///
|
|
|
|
/// Many configuration options are available, in order to be able to create
|
|
|
|
/// different rules for different kind of identifier. In general, the
|
|
|
|
/// rules are falling back to a more generic rule if the specific case is not
|
|
|
|
/// configured.
|
2020-01-17 05:29:19 +08:00
|
|
|
class IdentifierNamingCheck final : public RenamerClangTidyCheck {
|
2015-08-19 19:15:36 +08:00
|
|
|
public:
|
|
|
|
IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
|
2019-09-26 21:47:29 +08:00
|
|
|
~IdentifierNamingCheck();
|
2015-08-19 19:15:36 +08:00
|
|
|
|
|
|
|
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
|
|
|
|
|
|
|
|
enum CaseType {
|
|
|
|
CT_AnyCase = 0,
|
|
|
|
CT_LowerCase,
|
|
|
|
CT_CamelBack,
|
|
|
|
CT_UpperCase,
|
|
|
|
CT_CamelCase,
|
2016-07-20 20:28:38 +08:00
|
|
|
CT_CamelSnakeCase,
|
|
|
|
CT_CamelSnakeBack
|
2015-08-19 19:15:36 +08:00
|
|
|
};
|
|
|
|
|
2021-08-02 06:20:12 +08:00
|
|
|
enum HungarianPrefixType {
|
|
|
|
HPT_Off = 0,
|
|
|
|
HPT_On,
|
|
|
|
HPT_LowerCase,
|
|
|
|
HPT_CamelCase,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HungarianNotationOption {
|
|
|
|
HungarianNotationOption() : HPType(HungarianPrefixType::HPT_Off) {}
|
|
|
|
|
|
|
|
llvm::Optional<CaseType> Case;
|
|
|
|
HungarianPrefixType HPType;
|
|
|
|
llvm::StringMap<std::string> General;
|
|
|
|
llvm::StringMap<std::string> CString;
|
|
|
|
llvm::StringMap<std::string> PrimitiveType;
|
|
|
|
llvm::StringMap<std::string> UserDefinedType;
|
|
|
|
llvm::StringMap<std::string> DerivedType;
|
|
|
|
};
|
|
|
|
|
2015-08-19 19:15:36 +08:00
|
|
|
struct NamingStyle {
|
2017-03-22 20:49:58 +08:00
|
|
|
NamingStyle() = default;
|
2015-08-19 19:15:36 +08:00
|
|
|
|
2017-03-22 20:50:05 +08:00
|
|
|
NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix,
|
2021-08-02 06:20:12 +08:00
|
|
|
const std::string &Suffix, const std::string &IgnoredRegexpStr,
|
|
|
|
HungarianPrefixType HPType);
|
2020-11-25 09:18:38 +08:00
|
|
|
NamingStyle(const NamingStyle &O) = delete;
|
|
|
|
NamingStyle &operator=(NamingStyle &&O) = default;
|
|
|
|
NamingStyle(NamingStyle &&O) = default;
|
2015-08-19 19:15:36 +08:00
|
|
|
|
2017-03-22 20:49:58 +08:00
|
|
|
llvm::Optional<CaseType> Case;
|
2015-08-19 19:15:36 +08:00
|
|
|
std::string Prefix;
|
|
|
|
std::string Suffix;
|
2020-11-25 09:18:38 +08:00
|
|
|
// Store both compiled and non-compiled forms so original value can be
|
|
|
|
// serialized
|
|
|
|
llvm::Regex IgnoredRegexp;
|
|
|
|
std::string IgnoredRegexpStr;
|
2021-08-02 06:20:12 +08:00
|
|
|
|
|
|
|
HungarianPrefixType HPType;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HungarianNotation {
|
|
|
|
public:
|
|
|
|
bool checkOptionValid(int StyleKindIndex, StringRef StyleString) const;
|
|
|
|
bool isOptionEnabled(StringRef OptionKey,
|
|
|
|
const llvm::StringMap<std::string> &StrMap) const;
|
|
|
|
void loadDefaultConfig(
|
|
|
|
IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
|
|
|
|
void loadFileConfig(
|
|
|
|
const ClangTidyCheck::OptionsView &Options,
|
|
|
|
IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
|
|
|
|
|
|
|
|
bool removeDuplicatedPrefix(
|
|
|
|
SmallVector<StringRef, 8> &Words,
|
|
|
|
const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
|
|
|
|
|
|
|
|
std::string getPrefix(
|
|
|
|
const Decl *D,
|
|
|
|
const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
|
|
|
|
|
2021-09-05 23:37:27 +08:00
|
|
|
std::string getDataTypePrefix(
|
2021-08-02 06:20:12 +08:00
|
|
|
StringRef TypeName, const NamedDecl *ND,
|
|
|
|
const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
|
|
|
|
|
|
|
|
std::string getClassPrefix(
|
|
|
|
const CXXRecordDecl *CRD,
|
|
|
|
const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
|
|
|
|
|
|
|
|
std::string getEnumPrefix(const EnumConstantDecl *ECD) const;
|
|
|
|
std::string getDeclTypeName(const NamedDecl *ND) const;
|
2015-08-19 19:15:36 +08:00
|
|
|
};
|
|
|
|
|
2020-11-06 03:51:04 +08:00
|
|
|
struct FileStyle {
|
|
|
|
FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {}
|
|
|
|
FileStyle(SmallVectorImpl<Optional<NamingStyle>> &&Styles,
|
2021-08-02 06:20:12 +08:00
|
|
|
HungarianNotationOption HNOption, bool IgnoreMainLike)
|
|
|
|
: Styles(std::move(Styles)), HNOption(std::move(HNOption)),
|
|
|
|
IsActive(true), IgnoreMainLikeFunctions(IgnoreMainLike) {}
|
2020-11-06 03:51:04 +08:00
|
|
|
|
|
|
|
ArrayRef<Optional<NamingStyle>> getStyles() const {
|
|
|
|
assert(IsActive);
|
|
|
|
return Styles;
|
|
|
|
}
|
2021-08-02 06:20:12 +08:00
|
|
|
|
|
|
|
const HungarianNotationOption &getHNOption() const {
|
|
|
|
assert(IsActive);
|
|
|
|
return HNOption;
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:51:04 +08:00
|
|
|
bool isActive() const { return IsActive; }
|
|
|
|
bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SmallVector<Optional<NamingStyle>, 0> Styles;
|
2021-08-02 06:20:12 +08:00
|
|
|
HungarianNotationOption HNOption;
|
2020-11-06 03:51:04 +08:00
|
|
|
bool IsActive;
|
|
|
|
bool IgnoreMainLikeFunctions;
|
|
|
|
};
|
|
|
|
|
2021-08-02 06:20:12 +08:00
|
|
|
IdentifierNamingCheck::FileStyle
|
|
|
|
getFileStyleFromOptions(const ClangTidyCheck::OptionsView &Options) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
matchesStyle(StringRef Type, StringRef Name,
|
|
|
|
const IdentifierNamingCheck::NamingStyle &Style,
|
|
|
|
const IdentifierNamingCheck::HungarianNotationOption &HNOption,
|
|
|
|
const NamedDecl *Decl) const;
|
|
|
|
|
|
|
|
std::string
|
|
|
|
fixupWithCase(StringRef Type, StringRef Name, const Decl *D,
|
|
|
|
const IdentifierNamingCheck::NamingStyle &Style,
|
|
|
|
const IdentifierNamingCheck::HungarianNotationOption &HNOption,
|
|
|
|
IdentifierNamingCheck::CaseType Case) const;
|
|
|
|
|
|
|
|
std::string
|
|
|
|
fixupWithStyle(StringRef Type, StringRef Name,
|
|
|
|
const IdentifierNamingCheck::NamingStyle &Style,
|
|
|
|
const IdentifierNamingCheck::HungarianNotationOption &HNOption,
|
|
|
|
const Decl *D) const;
|
|
|
|
|
|
|
|
StyleKind findStyleKind(
|
|
|
|
const NamedDecl *D,
|
|
|
|
ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
|
|
|
|
bool IgnoreMainLikeFunctions) const;
|
|
|
|
|
|
|
|
llvm::Optional<RenamerClangTidyCheck::FailureInfo> getFailureInfo(
|
|
|
|
StringRef Type, StringRef Name, const NamedDecl *ND,
|
|
|
|
SourceLocation Location,
|
|
|
|
ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
|
|
|
|
const IdentifierNamingCheck::HungarianNotationOption &HNOption,
|
|
|
|
StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const;
|
|
|
|
|
|
|
|
bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl,
|
|
|
|
bool IncludeMainLike) const;
|
|
|
|
|
2015-09-28 16:59:12 +08:00
|
|
|
private:
|
2020-01-17 05:29:19 +08:00
|
|
|
llvm::Optional<FailureInfo>
|
2021-11-02 15:14:25 +08:00
|
|
|
getDeclFailureInfo(const NamedDecl *Decl,
|
2020-01-17 05:29:19 +08:00
|
|
|
const SourceManager &SM) const override;
|
|
|
|
llvm::Optional<FailureInfo>
|
2021-11-02 15:14:25 +08:00
|
|
|
getMacroFailureInfo(const Token &MacroNameTok,
|
2020-01-17 05:29:19 +08:00
|
|
|
const SourceManager &SM) const override;
|
2021-11-02 15:14:25 +08:00
|
|
|
DiagInfo getDiagInfo(const NamingCheckId &ID,
|
2020-01-17 05:29:19 +08:00
|
|
|
const NamingCheckFailure &Failure) const override;
|
|
|
|
|
2020-11-06 03:51:04 +08:00
|
|
|
const FileStyle &getStyleForFile(StringRef FileName) const;
|
2020-08-01 17:35:13 +08:00
|
|
|
|
|
|
|
/// Stores the style options as a vector, indexed by the specified \ref
|
|
|
|
/// StyleKind, for a given directory.
|
2020-11-06 03:51:04 +08:00
|
|
|
mutable llvm::StringMap<FileStyle> NamingStylesCache;
|
|
|
|
FileStyle *MainFileStyle;
|
2021-08-02 06:20:12 +08:00
|
|
|
ClangTidyContext *Context;
|
2020-08-01 17:35:13 +08:00
|
|
|
const std::string CheckName;
|
|
|
|
const bool GetConfigPerFile;
|
[clang-tidy] readability-identifier-naming disregards parameters restrictions on main like functions
Summary:
Typically most main functions have the signature:
```
int main(int argc, char *argv[])
```
To stick with convention when renaming parameters we should ignore the `argc` and `argv` names even if the parameter style says they should be renamed. This patch addresses this by checking all ParmVarDecls if they form part of a function with a signature that matches main `int name(int argc, char * argv[], (optional char *env[]))`
Reviewers: aaron.ballman, JonasToth, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: Mordante, merge_guards_bot, xazax.hun, kristof.beyls, cfe-commits
Tags: #clang, #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D73098
2020-01-28 07:46:53 +08:00
|
|
|
const bool IgnoreFailedSplit;
|
2021-08-02 06:20:12 +08:00
|
|
|
HungarianNotation HungarianNotation;
|
2015-08-19 19:15:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace readability
|
2020-07-11 17:10:59 +08:00
|
|
|
template <>
|
|
|
|
struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {
|
|
|
|
static llvm::ArrayRef<
|
|
|
|
std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
|
|
|
|
getEnumMapping();
|
|
|
|
};
|
2015-08-19 19:15:36 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
|