2014-08-13 21:57:57 +08:00
|
|
|
//===--- HeaderGuard.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
|
2014-08-13 21:57:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-10 00:52:33 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
|
2014-08-13 21:57:57 +08:00
|
|
|
|
2020-06-29 23:05:51 +08:00
|
|
|
#include "../ClangTidyCheck.h"
|
2020-02-26 01:41:32 +08:00
|
|
|
#include "../utils/FileExtensionsUtils.h"
|
2014-08-13 21:57:57 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2016-05-03 10:54:05 +08:00
|
|
|
namespace utils {
|
2014-08-13 21:57:57 +08:00
|
|
|
|
2015-08-28 02:01:58 +08:00
|
|
|
/// Finds and fixes header guards.
|
2016-08-26 13:59:53 +08:00
|
|
|
/// The check supports these options:
|
2020-03-05 00:55:03 +08:00
|
|
|
/// - `HeaderFileExtensions`: a semicolon-separated list of filename
|
|
|
|
/// extensions of header files (The filename extension should not contain
|
|
|
|
/// "." prefix). ";h;hh;hpp;hxx" by default.
|
|
|
|
///
|
2016-08-26 13:59:53 +08:00
|
|
|
/// For extension-less header files, using an empty string or leaving an
|
2020-03-05 00:55:03 +08:00
|
|
|
/// empty string between ";" if there are other filename extensions.
|
2014-08-13 21:57:57 +08:00
|
|
|
class HeaderGuardCheck : public ClangTidyCheck {
|
|
|
|
public:
|
2014-09-12 16:53:36 +08:00
|
|
|
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
|
2016-08-26 13:59:53 +08:00
|
|
|
: ClangTidyCheck(Name, Context),
|
2017-07-20 20:02:03 +08:00
|
|
|
RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
|
|
|
|
"HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
|
2020-02-26 01:41:32 +08:00
|
|
|
utils::parseFileExtensions(RawStringHeaderFileExtensions,
|
2020-03-05 00:55:03 +08:00
|
|
|
HeaderFileExtensions,
|
|
|
|
utils::defaultFileExtensionDelimiters());
|
2016-08-26 13:59:53 +08:00
|
|
|
}
|
2020-06-22 02:01:09 +08:00
|
|
|
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
|
2019-03-23 02:58:12 +08:00
|
|
|
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
|
|
|
|
Preprocessor *ModuleExpanderPP) override;
|
2014-08-13 21:57:57 +08:00
|
|
|
|
2016-06-17 19:43:33 +08:00
|
|
|
/// Returns ``true`` if the check should suggest inserting a trailing comment
|
|
|
|
/// on the ``#endif`` of the header guard. It will use the same name as
|
|
|
|
/// returned by ``HeaderGuardCheck::getHeaderGuard``.
|
2014-08-13 21:57:57 +08:00
|
|
|
virtual bool shouldSuggestEndifComment(StringRef Filename);
|
2016-06-17 19:43:33 +08:00
|
|
|
/// Returns ``true`` if the check should suggest changing an existing header
|
|
|
|
/// guard to the string returned by ``HeaderGuardCheck::getHeaderGuard``.
|
2014-08-13 21:57:57 +08:00
|
|
|
virtual bool shouldFixHeaderGuard(StringRef Filename);
|
2016-06-17 19:43:33 +08:00
|
|
|
/// Returns ``true`` if the check should add a header guard to the file
|
2014-08-13 21:57:57 +08:00
|
|
|
/// if it has none.
|
|
|
|
virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename);
|
2016-06-17 19:43:33 +08:00
|
|
|
/// Returns a replacement for the ``#endif`` line with a comment mentioning
|
|
|
|
/// \p HeaderGuard. The replacement should start with ``endif``.
|
2014-10-15 20:18:35 +08:00
|
|
|
virtual std::string formatEndIf(StringRef HeaderGuard);
|
2016-06-17 19:43:33 +08:00
|
|
|
/// Gets the canonical header guard for a file.
|
2014-08-13 21:57:57 +08:00
|
|
|
virtual std::string getHeaderGuard(StringRef Filename,
|
|
|
|
StringRef OldGuard = StringRef()) = 0;
|
2016-08-26 13:59:53 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string RawStringHeaderFileExtensions;
|
2020-02-26 01:41:32 +08:00
|
|
|
utils::FileExtensionsSet HeaderFileExtensions;
|
2014-08-13 21:57:57 +08:00
|
|
|
};
|
|
|
|
|
2016-05-03 10:54:05 +08:00
|
|
|
} // namespace utils
|
2014-08-13 21:57:57 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
|
|
|
|
2015-03-10 00:52:33 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
|