Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / externandnotdefined.cxx
blobdf5455d0dddf7fca624f3292c7e93a96f2558b30
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include <string>
12 #include "plugin.hxx"
14 // Having an extern prototype for a method in a module and not actually declaring that method is dodgy.
17 namespace {
19 class ExternAndNotDefined:
20 public RecursiveASTVisitor<ExternAndNotDefined>, public loplugin::Plugin
22 public:
23 explicit ExternAndNotDefined(loplugin::InstantiationData const & data): Plugin(data) {}
25 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
27 bool VisitFunctionDecl(const FunctionDecl * decl);
30 bool ExternAndNotDefined::VisitFunctionDecl(const FunctionDecl * functionDecl) {
31 if (ignoreLocation(functionDecl)) {
32 return true;
34 if (functionDecl->isDefined() || functionDecl->isPure()
35 || (functionDecl->getLinkageAndVisibility().getLinkage()
36 != ExternalLinkage)) {
37 return true;
39 //TODO, filtering out anything template for now:
40 if (functionDecl->isDependentContext()) {
41 return true;
43 CXXRecordDecl const * r = dyn_cast<CXXRecordDecl>(functionDecl->getDeclContext());
44 if (r != nullptr && r->getTemplateSpecializationKind() != TSK_Undeclared) {
45 return true;
47 // this is the bison/flex C API, it has to be defined this way
48 std::string functionName = functionDecl->getNameAsString();
49 if (functionName == "yyerror" || functionName == "YYWarning" || functionName == "yyparse" || functionName == "yylex") {
50 return true;
52 // see vcl/unx/gtk/app/gtksys.cxx, typename conflicts prevent using the right include
53 if (functionName == "gdk_x11_screen_get_screen_number") {
54 return true;
56 if (!compiler.getSourceManager().isInMainFile(functionDecl->getLocation()))
58 return true;
60 StringRef fileName { compiler.getSourceManager().getFilename(functionDecl->getLocation()) };
61 // the filters use some kind of dynamic loading stunt
62 if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/filter/qa/")) {
63 return true;
65 report(
66 DiagnosticsEngine::Warning,
67 "extern prototype in main file without definition",
68 functionDecl->getLocation())
69 << functionDecl->getSourceRange();
70 return true;
74 loplugin::Plugin::Registration< ExternAndNotDefined > X("externandnotdefined");
78 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */