Skip to content

Commit ce5553f

Browse files
committed
Apply suggestions from vbvictor.
1 parent 8c9d519 commit ce5553f

File tree

9 files changed

+39
-11
lines changed

9 files changed

+39
-11
lines changed

clang-tools-extra/clang-tidy/portability/AvoidPragmaOnceCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class PragmaOnceCallbacks : public PPCallbacks {
3131
}
3232
Str = Str.trim();
3333
if (Str.starts_with("once")) {
34-
Check->diag(Loc, "Avoid pragma once.");
34+
Check->diag(Loc,
35+
"avoid 'pragma once' directive; use include guards instead");
3536
}
3637
}
3738

clang-tools-extra/clang-tidy/portability/AvoidPragmaOnceCheck.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace clang::tidy::portability {
1515

16-
/// FIXME: Write a short description.
16+
/// Finds uses of ``#pragma once`` and suggests replacing them with standard
17+
/// include guards (``#ifndef``/``#define``/``#endif``) for improved
18+
/// portability.
1719
///
1820
/// For the user-facing documentation see:
1921
/// http://clang.llvm.org/extra/clang-tidy/checks/portability/avoid-pragma-once.html

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ New checks
145145
- New :doc:`portability-avoid-pragma-once
146146
<clang-tidy/checks/portability/avoid-pragma-once>` check.
147147

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

150151
New check aliases
151152
^^^^^^^^^^^^^^^^^

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ Clang-Tidy Checks
351351
:doc:`performance-type-promotion-in-math-fn <performance/type-promotion-in-math-fn>`, "Yes"
352352
:doc:`performance-unnecessary-copy-initialization <performance/unnecessary-copy-initialization>`, "Yes"
353353
:doc:`performance-unnecessary-value-param <performance/unnecessary-value-param>`, "Yes"
354-
:doc:`portability-avoid-pragma-once <portability/avoid-pragma-once>`, "Yes"
354+
:doc:`portability-avoid-pragma-once <portability/avoid-pragma-once>`,
355355
:doc:`portability-restrict-system-includes <portability/restrict-system-includes>`, "Yes"
356356
:doc:`portability-simd-intrinsics <portability/simd-intrinsics>`,
357357
:doc:`portability-std-allocator-const <portability/std-allocator-const>`,

clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-once.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
portability-avoid-pragma-once
44
=============================
55

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

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

10-
```
11-
#pragma once // Bad: Avoid pragma once.
12-
```
13+
Some older or specialized C/C++ compilers, particularly in embedded systems,
14+
may not fully support #pragma once.
15+
16+
Also it can fail in certain file system configurations,like network drives
17+
or complex symbolic links, potentially leading to compilation issues.
18+
19+
Consider the following header file:
20+
21+
.. code:: c++
22+
23+
// my_header.h
24+
#pragma once // warning: avoid 'pragma once' directive; use include guards instead
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# pragma once
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# pragma once
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// RUN: %check_clang_tidy %s portability-avoid-pragma-once %t \
22
// RUN: -- --header-filter='.*' -- -I%S/Inputs/avoid-pragma-once
33

4-
#include "lib.h"
5-
// CHECK-MESSAGES: lib.h:1:1: warning: Avoid pragma once.
4+
// #pragma once
5+
#include "lib0.h"
6+
// CHECK-MESSAGES: lib0.h:1:1: warning: avoid 'pragma once' directive; use include guards instead
7+
8+
9+
// # pragma once
10+
#include "lib1.h"
11+
// CHECK-MESSAGES: lib1.h:1:1: warning: avoid 'pragma once' directive; use include guards instead
12+
13+
// # pragma once
14+
#include "lib2.h"
15+
// CHECK-MESSAGES: lib2.h:1:1: warning: avoid 'pragma once' directive; use include guards instead

0 commit comments

Comments
 (0)