Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 1 | //===--- DefinitionsInHeadersCheck.cpp - clang-tidy------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "DefinitionsInHeadersCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace misc { |
| 18 | |
| 19 | namespace { |
| 20 | |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 21 | AST_MATCHER_P(NamedDecl, usesHeaderFileExtension, |
| 22 | utils::HeaderFileExtensionsSet, HeaderFileExtensions) { |
| 23 | return utils::isExpansionLocInHeaderFile( |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 24 | Node.getBeginLoc(), Finder->getASTContext().getSourceManager(), |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 25 | HeaderFileExtensions); |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | } // namespace |
| 29 | |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 30 | DefinitionsInHeadersCheck::DefinitionsInHeadersCheck(StringRef Name, |
| 31 | ClangTidyContext *Context) |
| 32 | : ClangTidyCheck(Name, Context), |
| 33 | UseHeaderFileExtension(Options.get("UseHeaderFileExtension", true)), |
Alexander Kornienko | b1c7432 | 2017-07-20 12:02:03 +0000 | [diff] [blame] | 34 | RawStringHeaderFileExtensions(Options.getLocalOrGlobal( |
| 35 | "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 36 | if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions, |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 37 | HeaderFileExtensions, ',')) { |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 38 | // FIXME: Find a more suitable way to handle invalid configuration |
| 39 | // options. |
| 40 | llvm::errs() << "Invalid header file extension: " |
| 41 | << RawStringHeaderFileExtensions << "\n"; |
| 42 | } |
| 43 | } |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 44 | |
| 45 | void DefinitionsInHeadersCheck::storeOptions( |
| 46 | ClangTidyOptions::OptionMap &Opts) { |
| 47 | Options.store(Opts, "UseHeaderFileExtension", UseHeaderFileExtension); |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 48 | Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions); |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) { |
Haojian Wu | 3d1d076 | 2016-02-08 16:05:39 +0000 | [diff] [blame] | 52 | if (!getLangOpts().CPlusPlus) |
| 53 | return; |
Haojian Wu | ba992cf | 2016-06-07 08:55:38 +0000 | [diff] [blame] | 54 | auto DefinitionMatcher = |
| 55 | anyOf(functionDecl(isDefinition(), unless(isDeleted())), |
| 56 | varDecl(isDefinition())); |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 57 | if (UseHeaderFileExtension) { |
Haojian Wu | ba992cf | 2016-06-07 08:55:38 +0000 | [diff] [blame] | 58 | Finder->addMatcher(namedDecl(DefinitionMatcher, |
| 59 | usesHeaderFileExtension(HeaderFileExtensions)) |
| 60 | .bind("name-decl"), |
| 61 | this); |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 62 | } else { |
| 63 | Finder->addMatcher( |
Haojian Wu | ba992cf | 2016-06-07 08:55:38 +0000 | [diff] [blame] | 64 | namedDecl(DefinitionMatcher, |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 65 | anyOf(usesHeaderFileExtension(HeaderFileExtensions), |
| 66 | unless(isExpansionInMainFile()))) |
| 67 | .bind("name-decl"), |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 68 | this); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void DefinitionsInHeadersCheck::check(const MatchFinder::MatchResult &Result) { |
Haojian Wu | e5d2779 | 2016-06-27 08:04:01 +0000 | [diff] [blame] | 73 | // Don't run the check in failing TUs. |
Alexander Kornienko | 385c2a3 | 2017-02-08 16:11:22 +0000 | [diff] [blame] | 74 | if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred()) |
Haojian Wu | e5d2779 | 2016-06-27 08:04:01 +0000 | [diff] [blame] | 75 | return; |
| 76 | |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 77 | // C++ [basic.def.odr] p6: |
| 78 | // There can be more than one definition of a class type, enumeration type, |
| 79 | // inline function with external linkage, class template, non-static function |
| 80 | // template, static data member of a class template, member function of a |
| 81 | // class template, or template specialization for which some template |
| 82 | // parameters are not specifiedin a program provided that each definition |
| 83 | // appears in a different translation unit, and provided the definitions |
| 84 | // satisfy the following requirements. |
| 85 | const auto *ND = Result.Nodes.getNodeAs<NamedDecl>("name-decl"); |
| 86 | assert(ND); |
Haojian Wu | 3d1d076 | 2016-02-08 16:05:39 +0000 | [diff] [blame] | 87 | if (ND->isInvalidDecl()) |
| 88 | return; |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 89 | |
| 90 | // Internal linkage variable definitions are ignored for now: |
| 91 | // const int a = 1; |
| 92 | // static int b = 1; |
| 93 | // |
| 94 | // Although these might also cause ODR violations, we can be less certain and |
| 95 | // should try to keep the false-positive rate down. |
Richard Smith | 2ccecb9 | 2017-09-22 23:47:20 +0000 | [diff] [blame] | 96 | // |
| 97 | // FIXME: Should declarations in anonymous namespaces get the same treatment |
| 98 | // as static / const declarations? |
| 99 | if (!ND->hasExternalFormalLinkage() && !ND->isInAnonymousNamespace()) |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 100 | return; |
| 101 | |
| 102 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) { |
| 103 | // Inline functions are allowed. |
| 104 | if (FD->isInlined()) |
| 105 | return; |
| 106 | // Function templates are allowed. |
| 107 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 108 | return; |
Haojian Wu | ead59ee | 2017-02-15 14:10:50 +0000 | [diff] [blame] | 109 | // Ignore instantiated functions. |
| 110 | if (FD->isTemplateInstantiation()) |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 111 | return; |
| 112 | // Member function of a class template and member function of a nested class |
| 113 | // in a class template are allowed. |
| 114 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 115 | const auto *DC = MD->getDeclContext(); |
| 116 | while (DC->isRecord()) { |
Haojian Wu | 29634fe | 2016-02-03 12:10:27 +0000 | [diff] [blame] | 117 | if (const auto *RD = dyn_cast<CXXRecordDecl>(DC)) { |
| 118 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 119 | return; |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 120 | if (RD->getDescribedClassTemplate()) |
| 121 | return; |
Haojian Wu | 29634fe | 2016-02-03 12:10:27 +0000 | [diff] [blame] | 122 | } |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 123 | DC = DC->getParent(); |
| 124 | } |
| 125 | } |
| 126 | |
Haojian Wu | eb006d3 | 2019-05-28 14:50:42 +0000 | [diff] [blame] | 127 | bool IsFullSpec = FD->getTemplateSpecializationKind() != TSK_Undeclared; |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 128 | diag(FD->getLocation(), |
Haojian Wu | be2588f | 2017-02-14 12:39:22 +0000 | [diff] [blame] | 129 | "%select{function|full function template specialization}0 %1 defined " |
| 130 | "in a header file; function definitions in header files can lead to " |
| 131 | "ODR violations") |
Haojian Wu | eb006d3 | 2019-05-28 14:50:42 +0000 | [diff] [blame] | 132 | << IsFullSpec << FD; |
| 133 | diag(FD->getLocation(), /*FixDescription=*/"make as 'inline'", |
| 134 | DiagnosticIDs::Note) |
Kadir Cetinkaya | 4a16c29 | 2019-09-05 08:11:21 +0000 | [diff] [blame] | 135 | << FixItHint::CreateInsertion(FD->getInnerLocStart(), "inline "); |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 136 | } else if (const auto *VD = dyn_cast<VarDecl>(ND)) { |
| 137 | // Static data members of a class template are allowed. |
| 138 | if (VD->getDeclContext()->isDependentContext() && VD->isStaticDataMember()) |
| 139 | return; |
Haojian Wu | ead59ee | 2017-02-15 14:10:50 +0000 | [diff] [blame] | 140 | // Ignore instantiated static data members of classes. |
| 141 | if (isTemplateInstantiation(VD->getTemplateSpecializationKind())) |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 142 | return; |
| 143 | // Ignore variable definition within function scope. |
| 144 | if (VD->hasLocalStorage() || VD->isStaticLocal()) |
| 145 | return; |
Gabor Horvath | 2990ac1 | 2017-06-28 12:47:35 +0000 | [diff] [blame] | 146 | // Ignore inline variables. |
| 147 | if (VD->isInline()) |
| 148 | return; |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 149 | |
| 150 | diag(VD->getLocation(), |
Benjamin Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 151 | "variable %0 defined in a header file; " |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 152 | "variable definitions in header files can lead to ODR violations") |
Benjamin Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 153 | << VD; |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | } // namespace misc |
| 158 | } // namespace tidy |
| 159 | } // namespace clang |