bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / externvar.cxx
blobeb20d3f36b0d82c8d6f1d369977101d63c3a4d12
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include "check.hxx"
13 #include "plugin.hxx"
15 // Find variable declarations at namespace scope that need not have external
16 // linkage.
18 namespace {
20 // It looks like Clang wrongly implements DR 4
21 // (<http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#4>) and treats
22 // a variable declared in an 'extern "..." {...}'-style linkage-specification as
23 // if it contained the 'extern' specifier:
24 bool hasExternalLinkage(VarDecl const * decl) {
25 if (decl->getLinkageAndVisibility().getLinkage() != ExternalLinkage) {
26 return false;
28 for (auto ctx = decl->getLexicalDeclContext();
29 ctx->getDeclKind() != Decl::TranslationUnit;
30 ctx = ctx->getLexicalParent())
32 if (auto ls = dyn_cast<LinkageSpecDecl>(ctx)) {
33 if (!ls->hasBraces()) {
34 return true;
36 if (auto prev = decl->getPreviousDecl()) {
37 return hasExternalLinkage(prev);
39 return !decl->isInAnonymousNamespace();
42 return true;
45 class ExternVar: public loplugin::FilteringPlugin<ExternVar>
47 public:
48 explicit ExternVar(loplugin::InstantiationData const & data): FilteringPlugin(data)
51 void run() override
52 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
54 bool VisitVarDecl(VarDecl const * decl) {
55 if (ignoreLocation(decl)) {
56 return true;
58 if (decl->isStaticDataMember()) {
59 return true;
61 if (!(decl->isFirstDecl()
62 && compiler.getSourceManager().isInMainFile(decl->getLocation())
63 && hasExternalLinkage(decl)))
65 return true;
67 auto def = decl->getDefinition();
68 if (def == nullptr) {
69 // Code like
71 // namespace { extern int v; }
72 // int f() { return sizeof(v); }
74 // is already handled by Clang itself with an error "variable 'v' is
75 // not needed and will not be emitted"
76 return true;
78 if (loplugin::DeclCheck(def).Var("_pRawDllMain").GlobalNamespace()) {
79 return true;
81 SourceLocation argLoc;
82 if (compiler.getSourceManager().isMacroArgExpansion(def->getLocation(), &argLoc)
83 && (Lexer::getImmediateMacroName(
84 argLoc, compiler.getSourceManager(), compiler.getLangOpts())
85 == "DEFINE_GUID"))
87 return true;
89 report(
90 DiagnosticsEngine::Warning,
91 "variable with external linkage not declared in an include file",
92 def->getLocation())
93 << def->getSourceRange();
94 report(
95 DiagnosticsEngine::Note,
96 ("should either have internal linkage or be declared in an include"
97 " file"),
98 def->getLocation())
99 << def->getSourceRange();
100 for (auto prev = def;;) {
101 prev = prev->getPreviousDecl();
102 if (prev == nullptr) {
103 break;
105 report(
106 DiagnosticsEngine::Note, "previously declared here",
107 prev->getLocation())
108 << prev->getSourceRange();
110 return true;
114 loplugin::Plugin::Registration<ExternVar> externvar("externvar");
118 #endif // LO_CLANG_SHARED_PLUGINS
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */