Skip to content

Commit 5bc074d

Browse files
committed
[clang-tidy] Add avoid-pragma-once.
1 parent 2f05451 commit 5bc074d

File tree

9 files changed

+111
-0
lines changed

9 files changed

+111
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===--- AvoidPragmaOnceCheck.cpp - clang-tidy ----------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "AvoidPragmaOnceCheck.h"
10+
11+
#include "clang/Basic/SourceManager.h"
12+
#include "clang/Lex/PPCallbacks.h"
13+
#include "clang/Lex/Preprocessor.h"
14+
#include "llvm/ADT/StringRef.h"
15+
16+
namespace clang::tidy::portability {
17+
18+
class PragmaOnceCallbacks : public PPCallbacks {
19+
public:
20+
PragmaOnceCallbacks(AvoidPragmaOnceCheck *Check, const SourceManager &SM)
21+
: Check(Check), SM(SM) {}
22+
void PragmaDirective(SourceLocation Loc,
23+
PragmaIntroducerKind Introducer) override {
24+
auto Str = llvm::StringRef(SM.getCharacterData(Loc));
25+
if (!Str.consume_front("#")) {
26+
return;
27+
}
28+
Str = Str.trim();
29+
if (!Str.consume_front("pragma")) {
30+
return;
31+
}
32+
Str = Str.trim();
33+
if (Str.starts_with("once")) {
34+
Check->diag(Loc, "Avoid pragma once.");
35+
}
36+
}
37+
38+
private:
39+
AvoidPragmaOnceCheck *Check;
40+
const SourceManager &SM;
41+
};
42+
43+
void AvoidPragmaOnceCheck::registerPPCallbacks(const SourceManager &SM,
44+
Preprocessor *PP,
45+
Preprocessor *ModuleExpanderPP) {
46+
PP->addPPCallbacks(std::make_unique<PragmaOnceCallbacks>(this, SM));
47+
}
48+
49+
} // namespace clang::tidy::portability
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- AvoidPragmaOnceCheck.h - clang-tidy --------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMAONCECHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMAONCECHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::portability {
15+
16+
/// FIXME: Write a short description.
17+
///
18+
/// For the user-facing documentation see:
19+
/// http://clang.llvm.org/extra/clang-tidy/checks/portability/avoid-pragma-once.html
20+
class AvoidPragmaOnceCheck : public ClangTidyCheck {
21+
public:
22+
AvoidPragmaOnceCheck(StringRef Name, ClangTidyContext *Context)
23+
: ClangTidyCheck(Name, Context) {}
24+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
25+
return LangOpts.CPlusPlus;
26+
}
27+
28+
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
29+
Preprocessor *ModuleExpanderPP) override;
30+
};
31+
32+
} // namespace clang::tidy::portability
33+
34+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_AVOIDPRAGMAONCECHECK_H

clang-tools-extra/clang-tidy/portability/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55
)
66

77
add_clang_library(clangTidyPortabilityModule STATIC
8+
AvoidPragmaOnceCheck.cpp
89
PortabilityTidyModule.cpp
910
RestrictSystemIncludesCheck.cpp
1011
SIMDIntrinsicsCheck.cpp

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../ClangTidy.h"
1010
#include "../ClangTidyModule.h"
1111
#include "../ClangTidyModuleRegistry.h"
12+
#include "AvoidPragmaOnceCheck.h"
1213
#include "RestrictSystemIncludesCheck.h"
1314
#include "SIMDIntrinsicsCheck.h"
1415
#include "StdAllocatorConstCheck.h"
@@ -20,6 +21,8 @@ namespace portability {
2021
class PortabilityModule : public ClangTidyModule {
2122
public:
2223
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
24+
CheckFactories.registerCheck<AvoidPragmaOnceCheck>(
25+
"portability-avoid-pragma-once");
2326
CheckFactories.registerCheck<RestrictSystemIncludesCheck>(
2427
"portability-restrict-system-includes");
2528
CheckFactories.registerCheck<SIMDIntrinsicsCheck>(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ New checks
142142
Finds potentially erroneous calls to ``reset`` method on smart pointers when
143143
the pointee type also has a ``reset`` method.
144144

145+
- New :doc:`portability-avoid-pragma-once
146+
<clang-tidy/checks/portability/avoid-pragma-once>` check.
147+
148+
A check that catches pragma once.
149+
145150
New check aliases
146151
^^^^^^^^^^^^^^^^^
147152

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +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"
354355
:doc:`portability-restrict-system-includes <portability/restrict-system-includes>`, "Yes"
355356
:doc:`portability-simd-intrinsics <portability/simd-intrinsics>`,
356357
:doc:`portability-std-allocator-const <portability/std-allocator-const>`,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. title:: clang-tidy - portability-avoid-pragma-once
2+
3+
portability-avoid-pragma-once
4+
=============================
5+
6+
This check catches pragma once usage.
7+
8+
For example:
9+
10+
```
11+
#pragma once // Bad: Avoid pragma once.
12+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#pragma once
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %check_clang_tidy %s portability-avoid-pragma-once %t \
2+
// RUN: -- -- -isystem %S/Inputs/avoid-pragma-once
3+
4+
#include <lib.h>
5+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: Avoid pragma once. [portability-avoid-pragma-once]

0 commit comments

Comments
 (0)