[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/vbvictor closed https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
vbvictor wrote: I hope it will not break any build bots, landing OS-specific tests/checks is a bit tricky. Crossing my fingers https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
vbvictor wrote: > Thanks for the reviews @vbvictor and @zwuis, I'll rebase and merge when CI > passes. It's just for easier PR assignment. I can merge this myself https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
t-a-james wrote: @zeyi2 , if I'm reading this correctly, you're listed as a code owner and I therefore need you to approve this too, please :) https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
vbvictor wrote: Please make github email public https://llvm.org/docs/DeveloperPolicy.html#email-addresses https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From cb8448b0054e402707be02464d9eed254d17653a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.md | 24 +
.../portability/avoid-pragma-comment.cpp | 49 ++
8 files changed, 171 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.md
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..8f11f9399f248
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on Linux, the rest are
+/// Windows-only and should be caught by "-Wunknown-pragmas" on Linux.
+///
+/// For the user-facing documentation see:
+///
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
t-a-james wrote: Thanks for the reviews @vbvictor and @zwuis, I'll rebase and merge when CI passes. https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From cb8448b0054e402707be02464d9eed254d17653a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.md | 24 +
.../portability/avoid-pragma-comment.cpp | 49 ++
8 files changed, 171 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.md
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..8f11f9399f248
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on Linux, the rest are
+/// Windows-only and should be caught by "-Wunknown-pragmas" on Linux.
+///
+/// For the user-facing documentation see:
+///
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/vbvictor approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
t-a-james wrote:
Just un-fixed this, and I'm going to assume your approval stands
https://github.com/llvm/llvm-project/pull/215239
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH 1/8] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
t-a-james wrote:
Wait no, bad idea!
On Windows, there are extra types (compiler, exestr, user, etc) that should hit
the `default: break;` case
https://github.com/llvm/llvm-project/pull/215239
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
@@ -0,0 +1,49 @@
+// Only the "lib" pragma comment type is implemented on Linux, the rest are
+// Windows-only. We test for both platform targets.
+// RUN: %check_clang_tidy -check-suffixes=LINUX %s
portability-avoid-pragma-comment %t -- -- -target x86_64-unknown-linux-gnu
+// RUN: %check_clang_tidy -check-suffixes=WINDOWS %s
portability-avoid-pragma-comment %t -- -- -target x86_64-pc-windows-msvc
+
+#pragma comment(lib, "some_lib")
+// CHECK-MESSAGES-LINUX: :[[@LINE-1]]:9: warning: avoid 'pragma comment'
directive; use the build system to link libraries
[portability-avoid-pragma-comment]
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-2]]:9: warning: avoid 'pragma comment'
directive; use the build system to link libraries
[portability-avoid-pragma-comment]
+
+_Pragma("comment(lib, \"some_lib\")")
+// CHECK-MESSAGES-LINUX: :[[@LINE-1]]:1: warning: avoid 'pragma comment'
directive; use the build system to link libraries
[portability-avoid-pragma-comment]
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-2]]:1: warning: avoid 'pragma comment'
directive; use the build system to link libraries
[portability-avoid-pragma-comment]
+
+// The rest are Windows-only and should be caught by "-Wunknown-pragmas" or
+// "-Wignored-pragmas" on Linux. On Linux they won't show up in the AST, so
+// portability-avoid-pragma-comment won't detect them.
+
+#pragma comment(linker, "some_linker_flag")
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:9: warning: avoid 'pragma comment'
directive; use the build system to set linker options
[portability-avoid-pragma-comment]
+
+#pragma comment(compiler)
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:9: warning: avoid 'pragma comment'
directive [portability-avoid-pragma-comment]
+
+#pragma comment(user, "Some string")
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:9: warning: avoid 'pragma comment'
directive [portability-avoid-pragma-comment]
+
+_Pragma("comment(linker, \"some_linker_flag\")")
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:1: warning: avoid 'pragma comment'
directive; use the build system to set linker options
[portability-avoid-pragma-comment]
+
+_Pragma("comment(compiler)")
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:1: warning: avoid 'pragma comment'
directive [portability-avoid-pragma-comment]
+
+_Pragma("comment(user, \"Some string\")")
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:1: warning: avoid 'pragma comment'
directive [portability-avoid-pragma-comment]
+
+// __pragma() is a Microsoft-specific extension
+#ifdef _MSC_VER
+__pragma(comment(lib, "some_lib"))
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:10: warning: avoid 'pragma comment'
directive; use the build system to link libraries
[portability-avoid-pragma-comment]
+
+__pragma(comment(linker, "some_linker_flag"))
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:10: warning: avoid 'pragma comment'
directive; use the build system to set linker options
[portability-avoid-pragma-comment]
+
+__pragma(comment(compiler))
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:10: warning: avoid 'pragma comment'
directive [portability-avoid-pragma-comment]
+
+__pragma(comment(user, "Some string"))
+// CHECK-MESSAGES-WINDOWS: :[[@LINE-1]]:10: warning: avoid 'pragma comment'
directive [portability-avoid-pragma-comment]
+#endif
t-a-james wrote:
Fixed!
https://github.com/llvm/llvm-project/pull/215239
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
t-a-james wrote:
Good idea, fixed in my latest push
https://github.com/llvm/llvm-project/pull/215239
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH 1/7] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH 1/6] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH 1/6] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH 1/6] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
t-a-james wrote: You're right, that's slightly tidier - fixed in my latest push! https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/zwuis edited https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/zwuis commented: LGTM if vbvictor or zeyi2 is happy. https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
zwuis wrote: It's up to you, but we can use `-check-suffixes` so that we can merge these two test files. https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on Linux, the rest are
+/// Windows-only and should be caught by "-Wunknown-pragmas" on Linux.
+///
+/// For the user-facing documentation see:
+///
https://clang.llvm.org/extra/clang-tidy/checks/portability/avoid-pragma-comment.html
+class AvoidPragmaCommentCheck : public ClangTidyCheck {
+public:
+ AvoidPragmaCommentCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus;
t-a-james wrote:
> I was thinking about this. So this part of motivation doesn't make sense to
> me :)
I think the current status is:
- MSVC supports all pragma comment types
- GCC doesn't support any
- Clang supports `lib` for Linux, and all types when targeting Windows
- I haven't checked other compilers
(You can see this in the test files :) )
In my situation the user was only using `lib`, which Clang accepts on both
Windows and Linux. I wanted to write a check to forbid `#pragma comment(lib,
some_lib)`, and when reading up on pragma comment realised there were a bunch
more types I should catch.
I've phrased this as "not widely supported outside of MSVC", because my guess
is that Clang only supports the different comment types for Windows because
it's trying to be compatible with MSVC. Outside of this MSVC-like context,
Clang only supports one of these comment types on Linux, and GNU doesn't
support any.
I use this phrase in
`clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.md`
- would you like me to rephrase that to make it clearer? :)
https://github.com/llvm/llvm-project/pull/215239
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on Linux, the rest are
+/// Windows-only and should be caught by "-Wunknown-pragmas" on Linux.
+///
+/// For the user-facing documentation see:
+///
https://clang.llvm.org/extra/clang-tidy/checks/portability/avoid-pragma-comment.html
+class AvoidPragmaCommentCheck : public ClangTidyCheck {
+public:
+ AvoidPragmaCommentCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus;
zwuis wrote:
> Is the `-Wunknown-pragma` compiler warning enough?
You are right.
> `#pragma comment` is not widely supported outside of MSVC.
I was thinking about this. So this part of motivation doesn't make sense to me
:)
---
> Regardless, I think `pragma comment` causes particular issues with
> portability. My project has quite complex dependency management tooling
> (which tends to be tightly tied to the build system) and `pragma comment`
> gives developers a way to sneak dependencies in through the back door.
Makes sense to me!
https://github.com/llvm/llvm-project/pull/215239
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
EugeneZelenko wrote: Please convert to Markdown format. https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH 1/4] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH 1/3] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH 1/2] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
github-actions[bot] wrote: :warning: RST documentation linter, doc8 found issues in your code. :warning: You can test this locally with the following command: ```bash doc8 -q clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst --config clang-tools-extra/clang-tidy/doc8.ini ``` View the output from doc8 here. ``` clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst:15: D000 Error in "code" directive: maximum 1 argument(s) allowed, 29 supplied. clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst:23: D005 No newline at end of file ``` https://github.com/llvm/llvm-project/pull/215239 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james updated
https://github.com/llvm/llvm-project/pull/215239
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clang-tools-extra
Author: Tom (t-a-james)
Changes
Finds uses of `#pragma comment` and, for `lib` or `linker` comments, suggests
using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang supports the
use of `#pragma comment` to link libraries on both Windows and Linux, but other
kinds are only supported on Windows. Using `pragma comment` to change link
flags may be unexpected in projects that prefer to set these flags in the build
system.
---
Full diff: https://github.com/llvm/llvm-project/pull/215239.diff
9 Files Affected:
- (added) clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
(+50)
- (added) clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
(+37)
- (modified) clang-tools-extra/clang-tidy/portability/CMakeLists.txt (+1)
- (modified) clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp
(+3)
- (modified) clang-tools-extra/docs/ReleaseNotes.md (+6)
- (modified) clang-tools-extra/docs/clang-tidy/checks/list.md (+1)
- (added)
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
(+23)
- (added)
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
(+21)
- (added)
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
(+42)
``diff
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on Linux, the rest are
+/// Windows-only and should be caught by "-Wunknown-pragmas" on Linux.
+///
+/// For the user-facing documentation see:
+///
https://clang.llvm.org/extra/clang-tidy/checks/portability/avoid-pragma-comment.html
+class AvoidPragmaCommentCheck : public ClangTidyCheck {
+public:
+ AvoidPragmaCommentCheck(StringRef Name, ClangTidyContext *Context)
+
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clang-tidy
Author: Tom (t-a-james)
Changes
Finds uses of `#pragma comment` and, for `lib` or `linker` comments, suggests
using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang supports the
use of `#pragma comment` to link libraries on both Windows and Linux, but other
kinds are only supported on Windows. Using `pragma comment` to change link
flags may be unexpected in projects that prefer to set these flags in the build
system.
---
Full diff: https://github.com/llvm/llvm-project/pull/215239.diff
9 Files Affected:
- (added) clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
(+50)
- (added) clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
(+37)
- (modified) clang-tools-extra/clang-tidy/portability/CMakeLists.txt (+1)
- (modified) clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp
(+3)
- (modified) clang-tools-extra/docs/ReleaseNotes.md (+6)
- (modified) clang-tools-extra/docs/clang-tidy/checks/list.md (+1)
- (added)
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
(+23)
- (added)
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
(+21)
- (added)
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
(+42)
``diff
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMACOMMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::portability {
+
+/// Finds uses of ``#pragma comment`` and for ``lib`` or ``linker`` comments
+/// suggests using the build system for improved portability.
+///
+/// Only the "lib" pragma comment type is implemented on Linux, the rest are
+/// Windows-only and should be caught by "-Wunknown-pragmas" on Linux.
+///
+/// For the user-facing documentation see:
+///
https://clang.llvm.org/extra/clang-tidy/checks/portability/avoid-pragma-comment.html
+class AvoidPragmaCommentCheck : public ClangTidyCheck {
+public:
+ AvoidPragmaCommentCheck(StringRef Name, ClangTidyContext *Context)
+ :
[clang-tools-extra] [clang-tidy] New portability-avoid-pragma-comment (PR #215239)
https://github.com/t-a-james created
https://github.com/llvm/llvm-project/pull/215239
Finds uses of `#pragma comment` and, for `lib` or `linker` comments, suggests
using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang supports the
use of `#pragma comment` to link libraries on both Windows and Linux, but other
kinds are only supported on Windows. Using `pragma comment` to change link
flags may be unexpected in projects that prefer to set these flags in the build
system.
>From 5c76f8d1f42b643dc4f6a8b912d798af210e745a Mon Sep 17 00:00:00 2001
From: Tom James
Date: Mon, 10 Aug 2026 11:52:40 +0100
Subject: [PATCH] [clang-tidy] New portability-avoid-pragma-comment
Finds uses of `#pragma comment` and, for `lib` or `linker` comments,
suggests using the build system for improved portability.
`#pragma comment` is not widely supported outside of MSVC. Clang
supports the use of `#pragma comment` to link libraries on both Windows
and Linux, but other kinds are only supported on Windows. Using `pragma
comment` to change link flags may be unexpected in projects that prefer
to set these flags in the build system.
---
.../portability/AvoidPragmaCommentCheck.cpp | 50 +++
.../portability/AvoidPragmaCommentCheck.h | 37 ++
.../clang-tidy/portability/CMakeLists.txt | 1 +
.../portability/PortabilityTidyModule.cpp | 3 ++
clang-tools-extra/docs/ReleaseNotes.md| 6 +++
.../docs/clang-tidy/checks/list.md| 1 +
.../portability/avoid-pragma-comment.rst | 23 +
.../avoid-pragma-comment-linux.cpp| 21
.../avoid-pragma-comment-windows.cpp | 42
9 files changed, 184 insertions(+)
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-comment.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-linux.cpp
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-comment-windows.cpp
diff --git
a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
new file mode 100644
index 0..1b2f5d92687b8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.cpp
@@ -0,0 +1,50 @@
+//===--===//
+//
+// 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 "AvoidPragmaCommentCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+static const internal::VariadicDynCastAllOfMatcher
+// All other node matchers declared in this way are camelCase
+// NOLINTNEXTLINE(readability-identifier-naming)
+pragmaCommentDecl;
+
+void AvoidPragmaCommentCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(pragmaCommentDecl().bind("pragma"), this);
+}
+
+void AvoidPragmaCommentCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Pragma = Result.Nodes.getNodeAs("pragma");
+
+ std::string Msg{"avoid 'pragma comment' directive"};
+
+ // We can give specific advice about comments that add linker flags, but
other
+ // kinds are too generic
+ const PragmaMSCommentKind &Kind = Pragma->getCommentKind();
+ switch (Kind) {
+ case PragmaMSCommentKind::PCK_Lib:
+Msg.append("; use the build system to link libraries");
+break;
+ case PragmaMSCommentKind::PCK_Linker:
+Msg.append("; use the build system to set linker options");
+break;
+ case PragmaMSCommentKind::PCK_Unknown:
+llvm_unreachable("unexpected pragma comment kind");
+ default:
+break;
+ }
+ diag(Pragma->getBeginLoc(), Msg);
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
new file mode 100644
index 0..47f326374b97a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/AvoidPragmaCommentCheck.h
@@ -0,0 +1,37 @@
+//===--===//
+//
+// 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
+//
