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