Skip to content

Commit

Permalink
Implement daedalean-unions-must-not-be-used check
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier-varez committed Jul 14, 2022
1 parent 9ff9048 commit 1045ae7
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/daedalean/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_clang_library(clangTidyDaedaleanModule
StructsAndClassesCheck.cpp
ProtectedMustNotBeUsedCheck.cpp
SwitchStatementCheck.cpp
UnionsMustNotBeUsedCheck.cpp

LINK_LIBS
clangTidy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "SwitchStatementCheck.h"
#include "TernaryOperatorMustNotBeUsedCheck.h"
#include "TypeConversionsCheck.h"
#include "UnionsMustNotBeUsedCheck.h"

using namespace clang::ast_matchers;

Expand Down Expand Up @@ -73,6 +74,8 @@ class DaedaleanModule : public ClangTidyModule {
"daedalean-ternary-operator-must-not-be-used");
CheckFactories.registerCheck<SwitchStatementCheck>(
"daedalean-switch-statement");
CheckFactories.registerCheck<UnionsMustNotBeUsedCheck>(
"daedalean-unions-must-not-be-used");
}
};

Expand Down
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 clang-tools-extra/clang-tidy/daedalean/UnionsMustNotBeUsedCheck.h
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
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ New checks
- New :doc:`daedalean-type-conversions
<clang-tidy/checks/daedalean-type-conversions>` check.

- New :doc:`daedalean-unions-must-not-be-used
<clang-tidy/checks/daedalean-unions-must-not-be-used>` check.

FIXME: add release notes.

- New :doc:`misc-misleading-bidirectional <clang-tidy/checks/misc-misleading-bidirectional>` check.

Inspects string literal and comments for unterminated bidirectional Unicode
Expand Down
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.
4 changes: 3 additions & 1 deletion clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ Clang-Tidy Checks
`daedalean-class-methods <daedalean-class-methods.html>`_,
`daedalean-derived-classes <daedalean-derived-classes.html>`_, "Yes"
`daedalean-enum-class <daedalean-enum-class.html>`_, "Yes"
`daedalean-type-conversions <daedalean-type-conversions.html>`_, "Yes"
`daedalean-floating-point-comparison <daedalean-floating-point-comparison.html>`_,
`daedalean-friend-declarations <daedalean-friend-declarations.html>`_,
`daedalean-include-order <daedalean-include-order.html>`_, "Yes"
Expand All @@ -187,6 +186,8 @@ Clang-Tidy Checks
`daedalean-structs-and-classes <daedalean-structs-and-classes.html>`_, "Yes"
`daedalean-switch-statement <daedalean-switch-statement.html>`_,
`daedalean-ternary-operator-must-not-be-used <daedalean-ternary-operator-must-not-be-used.html>`_,
`daedalean-type-conversions <daedalean-type-conversions.html>`_,
`daedalean-unions-must-not-be-used <daedalean-unions-must-not-be-used.html>`_, "Yes"
`darwin-avoid-spinlock <darwin-avoid-spinlock.html>`_,
`darwin-dispatch-once-nonstatic <darwin-dispatch-once-nonstatic.html>`_, "Yes"
`fuchsia-default-arguments-calls <fuchsia-default-arguments-calls.html>`_,
Expand Down Expand Up @@ -347,6 +348,7 @@ Clang-Tidy Checks
`readability-use-anyofallof <readability-use-anyofallof.html>`_,
`zircon-temporary-objects <zircon-temporary-objects.html>`_,


.. csv-table:: Aliases..
:header: "Name", "Redirect", "Offers fixes"

Expand Down
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;
};

0 comments on commit 1045ae7

Please sign in to comment.