forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement daedalean-unions-must-not-be-used check
- Loading branch information
1 parent
9ff9048
commit 1045ae7
Showing
8 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
32 changes: 32 additions & 0 deletions
32
clang-tools-extra/clang-tidy/daedalean/UnionsMustNotBeUsedCheck.cpp
This file contains 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,32 @@ | ||
//===--- UnionsMustNotBeUsedCheck.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 "UnionsMustNotBeUsedCheck.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang { | ||
namespace tidy { | ||
namespace daedalean { | ||
|
||
void UnionsMustNotBeUsedCheck::registerMatchers(MatchFinder *Finder) { | ||
Finder->addMatcher(recordDecl(isUnion()).bind("x"), this); | ||
} | ||
|
||
void UnionsMustNotBeUsedCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto *MatchedDecl = Result.Nodes.getNodeAs<RecordDecl>("x"); | ||
diag(MatchedDecl->getLocation(), "Unions must not be used"); | ||
diag(MatchedDecl->getLocation(), "Remove union %0", DiagnosticIDs::Note) | ||
<< MatchedDecl; | ||
} | ||
|
||
} // namespace daedalean | ||
} // namespace tidy | ||
} // namespace clang |
34 changes: 34 additions & 0 deletions
34
clang-tools-extra/clang-tidy/daedalean/UnionsMustNotBeUsedCheck.h
This file contains 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,34 @@ | ||
//===--- UnionsMustNotBeUsedCheck.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_DAEDALEAN_UNIONSMUSTNOTBEUSEDCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_DAEDALEAN_UNIONSMUSTNOTBEUSEDCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang { | ||
namespace tidy { | ||
namespace daedalean { | ||
|
||
/// Checks CS.R.29 and flags uses of unions | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/daedalean-unions-must-not-be-used.html | ||
class UnionsMustNotBeUsedCheck : public ClangTidyCheck { | ||
public: | ||
UnionsMustNotBeUsedCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
}; | ||
|
||
} // namespace daedalean | ||
} // namespace tidy | ||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_DAEDALEAN_UNIONSMUSTNOTBEUSEDCHECK_H |
This file contains 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
6 changes: 6 additions & 0 deletions
6
clang-tools-extra/docs/clang-tidy/checks/daedalean-unions-must-not-be-used.rst
This file contains 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,6 @@ | ||
.. title:: clang-tidy - daedalean-unions-must-not-be-used | ||
|
||
daedalean-unions-must-not-be-used | ||
================================= | ||
|
||
Flags uses of unions and warns about them. |
This file contains 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
18 changes: 18 additions & 0 deletions
18
clang-tools-extra/test/clang-tidy/checkers/daedalean-unions-must-not-be-used.cpp
This file contains 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,18 @@ | ||
// RUN: %check_clang_tidy %s daedalean-unions-must-not-be-used %t | ||
|
||
union UnionsAreNotOk { | ||
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: Unions must not be used [daedalean-unions-must-not-be-used] | ||
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: Remove union 'UnionsAreNotOk' | ||
int i; | ||
char *ptr; | ||
}; | ||
|
||
struct Fine { | ||
int i; | ||
char *ptr; | ||
}; | ||
|
||
class AlsoFine { | ||
int i; | ||
char *ptr; | ||
}; |