Skip to content

[clang-tidy] Add avoid-pragma-once. #140388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class PragmaOnceCallbacks : public PPCallbacks {
}
Str = Str.trim();
if (Str.starts_with("once")) {
Check->diag(Loc, "Avoid pragma once.");
Check->diag(Loc,
"avoid 'pragma once' directive; use include guards instead");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace clang::tidy::portability {

/// FIXME: Write a short description.
/// Finds uses of ``#pragma once`` and suggests replacing them with standard
/// include guards (``#ifndef``/``#define``/``#endif``) for improved
/// portability.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/portability/avoid-pragma-once.html
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ New checks
- New :doc:`portability-avoid-pragma-once
<clang-tidy/checks/portability/avoid-pragma-once>` check.

A check that catches pragma once.
Finds uses of ``#pragma once`` and suggests replacing them with standard
include guards (``#ifndef``/``#define``/``#endif``) for improved portability.

New check aliases
^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ Clang-Tidy Checks
:doc:`performance-type-promotion-in-math-fn <performance/type-promotion-in-math-fn>`, "Yes"
:doc:`performance-unnecessary-copy-initialization <performance/unnecessary-copy-initialization>`, "Yes"
:doc:`performance-unnecessary-value-param <performance/unnecessary-value-param>`, "Yes"
:doc:`portability-avoid-pragma-once <portability/avoid-pragma-once>`, "Yes"
:doc:`portability-avoid-pragma-once <portability/avoid-pragma-once>`,
:doc:`portability-restrict-system-includes <portability/restrict-system-includes>`, "Yes"
:doc:`portability-simd-intrinsics <portability/simd-intrinsics>`,
:doc:`portability-std-allocator-const <portability/std-allocator-const>`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
portability-avoid-pragma-once
=============================

This check catches pragma once usage.
Finds uses of ``#pragma once`` and suggests replacing them with standard
include guards (``#ifndef``/``#define``/``#endif``) for improved portability.

For example:
`#pragma once` is a non-standard extension, despite being widely supported
by modern compilers. Relying on it can lead to portability issues in
environments.

```
#pragma once // Bad: Avoid pragma once.
```
Some older or specialized C/C++ compilers, particularly in embedded systems,
may not fully support #pragma once.

Also it can fail in certain file system configurations,like network drives
or complex symbolic links, potentially leading to compilation issues.

Consider the following header file:

.. code:: c++

// my_header.h
#pragma once // warning: avoid 'pragma once' directive; use include guards instead
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# pragma once
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# pragma once
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// RUN: %check_clang_tidy %s portability-avoid-pragma-once %t \
// RUN: -- --header-filter='.*' -- -I%S/Inputs/avoid-pragma-once

#include "lib.h"
// CHECK-MESSAGES: lib.h:1:1: warning: Avoid pragma once.
// #pragma once
#include "lib0.h"
// CHECK-MESSAGES: lib0.h:1:1: warning: avoid 'pragma once' directive; use include guards instead


// # pragma once
#include "lib1.h"
// CHECK-MESSAGES: lib1.h:1:1: warning: avoid 'pragma once' directive; use include guards instead

// # pragma once
#include "lib2.h"
// CHECK-MESSAGES: lib2.h:1:1: warning: avoid 'pragma once' directive; use include guards instead
Loading