aboutsummaryrefslogtreecommitdiff
path: root/lib/StaticAnalyzer/Checkers/ClangSACheckerProvider.cpp
blob: 291f8e01f4b942270c2c9ce4ec659f7141331670 (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//===--- ClangSACheckerProvider.cpp - Clang SA Checkers Provider ----------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Defines the CheckerProvider for the checkers defined in
// libclangStaticAnalyzerCheckers.
//
//===----------------------------------------------------------------------===//

#include "ClangSACheckerProvider.h"
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/CheckerProvider.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/DenseSet.h"
#include "map"

using namespace clang;
using namespace ento;

namespace {

/// \brief Provider for all the checkers in libclangStaticAnalyzerCheckers.
class ClangSACheckerProvider : public CheckerProvider {
public:
  virtual void registerCheckers(CheckerManager &checkerMgr,
                              CheckerOptInfo *checkOpts, unsigned numCheckOpts);
  virtual void printHelp(llvm::raw_ostream &OS);
};

}

CheckerProvider *ento::createClangSACheckerProvider() {
  return new ClangSACheckerProvider();
}

namespace {

struct StaticCheckerInfoRec {
  const char *FullName;
  void (*RegFunc)(CheckerManager &mgr);
  const char *HelpText;
  int GroupIndex;
  bool Hidden;
};

struct StaticPackageInfoRec {
  const char *FullName;
  int GroupIndex;
  bool Hidden;
};

struct StaticGroupInfoRec {
  const char *FullName;
};

} // end anonymous namespace.

static const StaticPackageInfoRec StaticPackageInfo[] = {
#define GET_PACKAGES
#define PACKAGE(FULLNAME, GROUPINDEX, HIDDEN)    \
  { FULLNAME, GROUPINDEX, HIDDEN },
#include "Checkers.inc"
  { 0, -1, 0 }
#undef PACKAGE
#undef GET_PACKAGES
};

static const unsigned NumPackages =   sizeof(StaticPackageInfo)
                                    / sizeof(StaticPackageInfoRec) - 1;

static const StaticGroupInfoRec StaticGroupInfo[] = {
#define GET_GROUPS
#define GROUP(FULLNAME)    \
  { FULLNAME },
#include "Checkers.inc"
  { 0 }
#undef GROUP
#undef GET_GROUPS
};

static const unsigned NumGroups =   sizeof(StaticGroupInfo)
                                    / sizeof(StaticGroupInfoRec) - 1;

static const StaticCheckerInfoRec StaticCheckerInfo[] = {
#define GET_CHECKERS
#define CHECKER(FULLNAME,CLASS,DESCFILE,HELPTEXT,GROUPINDEX,HIDDEN)    \
  { FULLNAME, register##CLASS, HELPTEXT, GROUPINDEX, HIDDEN },
#include "Checkers.inc"
  { 0, 0, 0, -1, 0}
#undef CHECKER
#undef GET_CHECKERS
};

static const unsigned NumCheckers =   sizeof(StaticCheckerInfo)
                                    / sizeof(StaticCheckerInfoRec) - 1;

namespace {

struct CheckNameOption {
  const char  *Name;
  const short *Members;
  const short *SubGroups;
  bool Hidden;
};

} // end anonymous namespace.

#define GET_MEMBER_ARRAYS
#include "Checkers.inc"
#undef GET_MEMBER_ARRAYS

// The table of check name options, sorted by name for fast binary lookup.
static const CheckNameOption CheckNameTable[] = {
#define GET_CHECKNAME_TABLE
#include "Checkers.inc"
#undef GET_CHECKNAME_TABLE
};
static const size_t
        CheckNameTableSize = sizeof(CheckNameTable) / sizeof(CheckNameTable[0]);

static bool CheckNameOptionCompare(const CheckNameOption &LHS,
                                   const CheckNameOption &RHS) {
  return strcmp(LHS.Name, RHS.Name) < 0;
}

static void collectCheckers(const CheckNameOption *checkName,
                            bool enable,
                         llvm::DenseSet<const StaticCheckerInfoRec *> &checkers,
                            bool collectHidden) {
  if (checkName->Hidden && !collectHidden)
    return;

  if (const short *member = checkName->Members) {
    if (enable) {
      for (; *member != -1; ++member)
        if (collectHidden || !StaticCheckerInfo[*member].Hidden)
          checkers.insert(&StaticCheckerInfo[*member]);
    } else {
      for (; *member != -1; ++member)
        checkers.erase(&StaticCheckerInfo[*member]);
    }
  }

  // Enable/disable all subgroups along with this one.
  if (const short *subGroups = checkName->SubGroups) {
    for (; *subGroups != -1; ++subGroups) {
      const CheckNameOption *sub = &CheckNameTable[*subGroups];
      collectCheckers(sub, enable, checkers, collectHidden && !sub->Hidden);
    }
  }
}

static void collectCheckers(CheckerOptInfo &opt,
                       llvm::DenseSet<const StaticCheckerInfoRec *> &checkers) {
  const char *optName = opt.getName();
  CheckNameOption key = { optName, 0, 0, false };
  const CheckNameOption *found =
  std::lower_bound(CheckNameTable, CheckNameTable + CheckNameTableSize, key,
                   CheckNameOptionCompare);
  if (found == CheckNameTable + CheckNameTableSize ||
      strcmp(found->Name, optName) != 0)
    return;  // Check name not found.

  opt.claim();
  collectCheckers(found, opt.isEnabled(), checkers, /*collectHidden=*/true);
}

void ClangSACheckerProvider::registerCheckers(CheckerManager &checkerMgr,
                             CheckerOptInfo *checkOpts, unsigned numCheckOpts) {
  llvm::DenseSet<const StaticCheckerInfoRec *> enabledCheckers;
  for (unsigned i = 0; i != numCheckOpts; ++i)
    collectCheckers(checkOpts[i], enabledCheckers);
  for (llvm::DenseSet<const StaticCheckerInfoRec *>::iterator
         I = enabledCheckers.begin(), E = enabledCheckers.end(); I != E; ++I) {
    (*I)->RegFunc(checkerMgr);
  }
}

//===----------------------------------------------------------------------===//
// Printing Help.
//===----------------------------------------------------------------------===//

static void printPackageOption(llvm::raw_ostream &OS) {
  // Find the maximum option length.
  unsigned OptionFieldWidth = 0;
  for (unsigned i = 0; i != NumPackages; ++i) {
    // Limit the amount of padding we are willing to give up for alignment.
    unsigned Length = strlen(StaticPackageInfo[i].FullName);
    if (Length <= 30)
      OptionFieldWidth = std::max(OptionFieldWidth, Length);
  }

  const unsigned InitialPad = 2;
  for (unsigned i = 0; i != NumPackages; ++i) {
    const StaticPackageInfoRec &package = StaticPackageInfo[i];
    const std::string &Option = package.FullName;
    int Pad = OptionFieldWidth - int(Option.size());
    OS.indent(InitialPad) << Option;

    if (package.GroupIndex != -1 || package.Hidden) {
      // Break on long option names.
      if (Pad < 0) {
        OS << "\n";
        Pad = OptionFieldWidth + InitialPad;
      }
      OS.indent(Pad + 1) << "[";
      if (package.GroupIndex != -1) {
        OS << "Group=" << StaticGroupInfo[package.GroupIndex].FullName;
        if (package.Hidden)
          OS << ", ";
      }
      if (package.Hidden)
        OS << "Hidden";
      OS << "]";
    }

    OS << "\n";
  }
}

typedef std::map<std::string, const StaticCheckerInfoRec *> SortedCheckers;

static void printCheckerOption(llvm::raw_ostream &OS,SortedCheckers &checkers) {
  // Find the maximum option length.
  unsigned OptionFieldWidth = 0;
  for (SortedCheckers::iterator
         I = checkers.begin(), E = checkers.end(); I != E; ++I) {
    // Limit the amount of padding we are willing to give up for alignment.
    unsigned Length = strlen(I->second->FullName);
    if (Length <= 30)
      OptionFieldWidth = std::max(OptionFieldWidth, Length);
  }

  const unsigned InitialPad = 2;
  for (SortedCheckers::iterator
         I = checkers.begin(), E = checkers.end(); I != E; ++I) {
    const std::string &Option = I->first;
    const StaticCheckerInfoRec &checker = *I->second;
    int Pad = OptionFieldWidth - int(Option.size());
    OS.indent(InitialPad) << Option;

    // Break on long option names.
    if (Pad < 0) {
      OS << "\n";
      Pad = OptionFieldWidth + InitialPad;
    }
    OS.indent(Pad + 1) << checker.HelpText;

    if (checker.GroupIndex != -1 || checker.Hidden) {
      OS << "  [";
      if (checker.GroupIndex != -1) {
        OS << "Group=" << StaticGroupInfo[checker.GroupIndex].FullName;
        if (checker.Hidden)
          OS << ", ";
      }
      if (checker.Hidden)
        OS << "Hidden";
      OS << "]";
    }

    OS << "\n";
  }
}

void ClangSACheckerProvider::printHelp(llvm::raw_ostream &OS) {
  OS << "USAGE: -analyzer-checker <CHECKER or PACKAGE or GROUP,...>\n";

  OS << "\nGROUPS:\n";
  for (unsigned i = 0; i != NumGroups; ++i)
    OS.indent(2) << StaticGroupInfo[i].FullName << "\n";

  OS << "\nPACKAGES:\n";
  printPackageOption(OS);

  OS << "\nCHECKERS:\n";

  // Sort checkers according to their full name.
  SortedCheckers checkers;
  for (unsigned i = 0; i != NumCheckers; ++i)
    checkers[StaticCheckerInfo[i].FullName] = &StaticCheckerInfo[i];

  printCheckerOption(OS, checkers);
}