summaryrefslogtreecommitdiffstats
path: root/build/clang-plugin/NonTrivialTypeInFfiChecker.cpp
blob: f482fb131f3b684a036403c9dced916342268cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "NonTrivialTypeInFfiChecker.h"
#include "CustomMatchers.h"

void NonTrivialTypeInFfiChecker::registerMatchers(MatchFinder *AstMatcher) {
  AstMatcher->addMatcher(functionDecl(isExternC()).bind("func"), this);
}

void NonTrivialTypeInFfiChecker::check(const MatchFinder::MatchResult &Result) {
  static DenseSet<const FunctionDecl *> CheckedFunctionDecls;

  const FunctionDecl *func = Result.Nodes.getNodeAs<FunctionDecl>("func");
  // Don't report errors on the same declarations more than once.
  if (!CheckedFunctionDecls.insert(func).second) {
    return;
  }

  if (inThirdPartyPath(func)) {
    return;
  }

  auto NoteFor = [](const QualType &T) -> std::string {
    std::string s = "Please consider using a pointer or reference";
    if (T->getAs<TemplateSpecializationType>()) {
      s += ", or explicitly instantiating the template";
    }
    return s + " instead";
  };

  for (ParmVarDecl *p : func->parameters()) {
    QualType T = p->getType().getUnqualifiedType();
    if (!T->isVoidType() && !T->isReferenceType() &&
        !T.isTriviallyCopyableType(*Result.Context)) {
      diag(p->getLocation(),
           "Type %0 must not be used as parameter to extern "
           "\"C\" function",
           DiagnosticIDs::Error)
          << T;
      diag(p->getLocation(), NoteFor(T), DiagnosticIDs::Note);
    }
  }

  QualType T = func->getReturnType().getUnqualifiedType();
  if (!T->isVoidType() && !T->isReferenceType() &&
      !T.isTriviallyCopyableType(*Result.Context)) {
    diag(func->getLocation(),
         "Type %0 must not be used as return type of "
         "extern \"C\" function",
         DiagnosticIDs::Error)
        << T;
    diag(func->getLocation(), NoteFor(T), DiagnosticIDs::Note);
  }
}