[clang-tools-extra] [llvm] New check `misc-header-guard` (PR #177315)
Yanzuo Liu via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 22 01:16:07 PST 2026
================
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "HeaderGuardCheck.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/Support/Path.h"
+
+namespace clang::tidy::misc {
+
+HeaderGuardCheck::HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
+ : clang::tidy::utils::HeaderGuardCheck(Name, Context),
+ HeaderDirs(utils::options::parseStringList(
+ Options.get("HeaderDirs", "include"))),
+ Prefix(Options.get("Prefix", "")) {}
+
+std::string HeaderGuardCheck::getHeaderGuard(StringRef Filename,
+ StringRef OldGuard) {
+ std::string Guard = tooling::getAbsolutePath(Filename);
+
+ // When running under Windows, need to convert the path separators from
+ // `\` to `/`.
+ Guard = llvm::sys::path::convert_to_slash(Guard);
+
+ // consider all directories from HeaderDirs option. Stop at first found.
+ for (const StringRef HeaderDir : HeaderDirs) {
+ const size_t PosHeaderDir = Guard.rfind(HeaderDir.str() + "/");
----------------
zwuis wrote:
It seems that we should add `/` before `HeaderDir`.
```cpp
"/" + HeaderDir.str() + "/"
```
https://github.com/llvm/llvm-project/pull/177315
More information about the llvm-commits
mailing list