-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
Open
dl8sd11
wants to merge
9
commits into
llvm:main
Choose a base branch
from
dl8sd11:avoid-pragma-once
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5bc074d
[clang-tidy] Add avoid-pragma-once.
dl8sd11 63ae6d7
Apply clang-format.
dl8sd11 80899c5
Fix test by adding header-filter.
dl8sd11 8c9d519
Fix test message location.
dl8sd11 ce5553f
Apply suggestions from vbvictor.
dl8sd11 ccf539e
Apply suggestions from EugeneZelenko.
dl8sd11 12a6d58
Apply suggestions from vbvictor.
dl8sd11 319681d
Apply suggestions from PiotrZSL.
dl8sd11 7570870
Improve the document.
dl8sd11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
clang-tools-extra/clang-tidy/portability/AvoidPragmaOnceCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//===--- AvoidPragmaOnceCheck.cpp - clang-tidy ----------------------------===// | ||
// | ||
// 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 "AvoidPragmaOnceCheck.h" | ||
|
||
#include "clang/Basic/SourceManager.h" | ||
#include "clang/Lex/PPCallbacks.h" | ||
#include "clang/Lex/Preprocessor.h" | ||
#include "llvm/ADT/StringRef.h" | ||
|
||
namespace clang::tidy::portability { | ||
|
||
class PragmaOnceCallbacks : public PPCallbacks { | ||
public: | ||
PragmaOnceCallbacks(AvoidPragmaOnceCheck *Check, const SourceManager &SM) | ||
: Check(Check), SM(SM) {} | ||
void PragmaDirective(SourceLocation Loc, | ||
PragmaIntroducerKind Introducer) override { | ||
auto Str = llvm::StringRef(SM.getCharacterData(Loc)); | ||
if (!Str.consume_front("#")) | ||
return; | ||
Str = Str.trim(); | ||
if (!Str.consume_front("pragma")) | ||
return; | ||
Str = Str.trim(); | ||
if (Str.starts_with("once")) | ||
Check->diag(Loc, | ||
"avoid 'pragma once' directive; use include guards instead"); | ||
} | ||
|
||
private: | ||
AvoidPragmaOnceCheck *Check; | ||
const SourceManager &SM; | ||
}; | ||
|
||
void AvoidPragmaOnceCheck::registerPPCallbacks(const SourceManager &SM, | ||
Preprocessor *PP, | ||
Preprocessor *ModuleExpanderPP) { | ||
PP->addPPCallbacks(std::make_unique<PragmaOnceCallbacks>(this, SM)); | ||
} | ||
|
||
} // namespace clang::tidy::portability |
36 changes: 36 additions & 0 deletions
36
clang-tools-extra/clang-tidy/portability/AvoidPragmaOnceCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===--- AvoidPragmaOnceCheck.h - clang-tidy --------------------*- C++ -*-===// | ||
// | ||
// 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_AVOIDPRAGMAONCECHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMAONCECHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::portability { | ||
|
||
/// 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 | ||
class AvoidPragmaOnceCheck : public ClangTidyCheck { | ||
public: | ||
AvoidPragmaOnceCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus || LangOpts.C99; | ||
} | ||
|
||
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, | ||
Preprocessor *ModuleExpanderPP) override; | ||
}; | ||
|
||
} // namespace clang::tidy::portability | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMAONCECHECK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
clang-tools-extra/docs/clang-tidy/checks/portability/avoid-pragma-once.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.. title:: clang-tidy - portability-avoid-pragma-once | ||
|
||
portability-avoid-pragma-once | ||
============================= | ||
|
||
Finds uses of ``#pragma once`` and suggests replacing them with standard | ||
include guards (``#ifndef``/``#define``/``#endif``) for improved portability. | ||
|
||
``#pragma once`` is a non-standard extension, despite being widely supported | ||
by modern compilers. Relying on it can lead to portability issues in | ||
some environments. | ||
|
||
Some older or specialized C/C++ compilers, particularly in embedded systems, | ||
may not fully support ``#pragma once``. | ||
|
||
It can also 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 | ||
dl8sd11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
The warning suggests using include guards: | ||
|
||
.. code:: c++ | ||
|
||
// my_header.h | ||
#ifndef PATH_TO_MY_HEADER_H // Good: use include guards. | ||
#define PATH_TO_MY_HEADER_H | ||
|
||
#endif // PATH_TO_MY_HEADER_H |
1 change: 1 addition & 0 deletions
1
clang-tools-extra/test/clang-tidy/checkers/portability/Inputs/avoid-pragma-once/lib0.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#pragma once |
1 change: 1 addition & 0 deletions
1
clang-tools-extra/test/clang-tidy/checkers/portability/Inputs/avoid-pragma-once/lib1.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# pragma once |
1 change: 1 addition & 0 deletions
1
clang-tools-extra/test/clang-tidy/checkers/portability/Inputs/avoid-pragma-once/lib2.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# pragma once |
15 changes: 15 additions & 0 deletions
15
clang-tools-extra/test/clang-tidy/checkers/portability/avoid-pragma-once.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// RUN: %check_clang_tidy %s portability-avoid-pragma-once %t \ | ||
// RUN: -- --header-filter='.*' -- -I%S/Inputs/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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.