File tree Expand file tree Collapse file tree 9 files changed +39
-11
lines changed Expand file tree Collapse file tree 9 files changed +39
-11
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1313
1414namespace 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
Original file line number Diff line number Diff 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
150151New check aliases
151152^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff 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 >`,
Original file line number Diff line number Diff line change 33portability-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
File renamed without changes.
Original file line number Diff line number Diff line change 1+ # pragma once
Original file line number Diff line number Diff line change 1+ # pragma once
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments