loplugin:external: Don't warn for injected friend functions

Change-Id: I35c0930f6ab8ae5d96e433958cf29791c78d5e31
Reviewed-on: https://gerrit.libreoffice.org/82802
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
diff --git a/compilerplugins/clang/external.cxx b/compilerplugins/clang/external.cxx
index 3248df5..2b6b689 100644
--- a/compilerplugins/clang/external.cxx
+++ b/compilerplugins/clang/external.cxx
@@ -32,6 +32,19 @@ bool derivesFromTestFixture(CXXRecordDecl const* decl)
           || std::any_of(decl->vbases_begin(), decl->vbases_end(), pred);
}

bool isInjectedFunction(FunctionDecl const* decl)
{
    for (auto d = decl->redecls_begin(); d != decl->redecls_end(); ++d)
    {
        auto const c = d->getLexicalDeclContext();
        if (!(c->isFunctionOrMethod() || c->isRecord()))
        {
            return false;
        }
    }
    return true;
}

class External : public loplugin::FilteringPlugin<External>
{
public:
@@ -146,6 +159,10 @@ public:
            //  #pragma GCC diagnostic ignored "-Wunused-function"
            return true;
        }
        if (isInjectedFunction(decl))
        {
            return true;
        }
        return handleDeclaration(decl);
    }

@@ -195,6 +212,10 @@ public:
        {
            return true;
        }
        if (isInjectedFunction(decl->getTemplatedDecl()))
        {
            return true;
        }
        return handleDeclaration(decl);
    }

diff --git a/compilerplugins/clang/test/external.cxx b/compilerplugins/clang/test/external.cxx
index fd7d558..ff996f5 100644
--- a/compilerplugins/clang/test/external.cxx
+++ b/compilerplugins/clang/test/external.cxx
@@ -16,10 +16,37 @@ int const n2 = 0; // no warning, internal linkage

constexpr int n3 = 0; // no warning, internal linkage

struct S1
{
    friend void f1() {} // no warning for injected function (no place where to mark it `static`)
    template <typename> friend void ft1() {} // ...nor for injected function template
    // expected-error@+1 {{externally available entity 'f2' is not previously declared in an included file (if it is only used in this translation unit, make it static; otherwise, provide a declaration of it in an included file) [loplugin:external]}}
    friend void f2() {}
};

struct S2
{
    friend void f1();
    template <typename> friend void ft1();
    // expected-note@+1 {{another declaration is here [loplugin:external]}}
    friend void f2();
};

static void g()
{
    void f1();
    // expected-note@+1 {{another declaration is here [loplugin:external]}}
    void f2();
}

// expected-note@+1 {{another declaration is here [loplugin:external]}}
void f2();

int main()
{
    (void)n2;
    (void)n3;
    g();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */