blob: c446bfd17fd7f0d426d1d6d2265a7df93946c12e [file] [log] [blame]
John Thompson4f8ba652013-03-12 02:07:30 +00001//===- tools/clang/Modularize.cpp - Check modularized headers -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a tool that checks whether a set of headers provides
11// the consistent definitions required to use modules. For example, it detects
12// whether the same entity (say, a NULL macro or size_t typedef) is defined in
13// multiple headers or whether a header produces different definitions under
14// different circumstances. These conditions cause modules built from the
15// headers to behave poorly, and should be fixed before introducing a module
16// map.
17//
18// Modularize takes as argument a file name for a file containing the
19// newline-separated list of headers to check with respect to each other.
20// Modularize also accepts regular front-end arguments.
21//
22// Usage: modularize (include-files_list) [(front-end-options) ...]
23//
24// Modularize will do normal parsing, reporting normal errors and warnings,
25// but will also report special error messages like the following:
26//
27// error: '(symbol)' defined at both (file):(row):(column) and
28// (file):(row):(column)
29//
30// error: header '(file)' has different contents dependening on how it was
31// included
32//
33// The latter might be followed by messages like the following:
34//
35// note: '(symbol)' in (file) at (row):(column) not always provided
36//
37//===----------------------------------------------------------------------===//
38
39#include "llvm/Config/config.h"
40#include "llvm/Support/FileSystem.h"
41#include "llvm/ADT/StringRef.h"
42#include "clang/Basic/SourceManager.h"
43#include "clang/Lex/Preprocessor.h"
44#include "clang/AST/ASTConsumer.h"
45#include "clang/AST/ASTContext.h"
46#include "clang/AST/RecursiveASTVisitor.h"
47#include "clang/Frontend/CompilerInstance.h"
48#include "clang/Frontend/FrontendActions.h"
49#include "clang/Tooling/CompilationDatabase.h"
50#include "clang/Tooling/Tooling.h"
51#include <vector>
52#include <string>
53#include <fstream>
54#include <algorithm>
55#include <iterator>
56
57using namespace clang::tooling;
58using namespace clang;
59using llvm::StringRef;
60
61struct Location {
62 const FileEntry *File;
63 unsigned Line, Column;
64
65 Location() : File(), Line(), Column() { }
66
67 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
68 Loc = SM.getExpansionLoc(Loc);
69 if (Loc.isInvalid())
70 return;
71
72 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
73 File = SM.getFileEntryForID(Decomposed.first);
74 if (!File)
75 return;
76
77 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
78 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
79 }
80
81 operator bool() const { return File != 0; }
82
83 friend bool operator==(const Location &X, const Location &Y) {
84 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
85 }
86
87 friend bool operator!=(const Location &X, const Location &Y) {
88 return !(X == Y);
89 }
90
91 friend bool operator<(const Location &X, const Location &Y) {
92 if (X.File != Y.File)
93 return X.File < Y.File;
94 if (X.Line != Y.Line)
95 return X.Line < Y.Line;
96 return X.Column < Y.Column;
97 }
98 friend bool operator>(const Location &X, const Location &Y) {
99 return Y < X;
100 }
101 friend bool operator<=(const Location &X, const Location &Y) {
102 return !(Y < X);
103 }
104 friend bool operator>=(const Location &X, const Location &Y) {
105 return !(X < Y);
106 }
107
108};
109
110
111struct Entry {
112 enum Kind {
113 Tag,
114 Value,
115 Macro
116 } Kind;
117
118 Location Loc;
119};
120
121struct HeaderEntry {
122 std::string Name;
123 Location Loc;
124
125 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
126 return X.Loc == Y.Loc && X.Name == Y.Name;
127 }
128 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
129 return !(X == Y);
130 }
131 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
132 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
133 }
134 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
135 return Y < X;
136 }
137 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
138 return !(Y < X);
139 }
140 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
141 return !(X < Y);
142 }
143};
144
145typedef std::vector<HeaderEntry> HeaderContents;
146
147class EntityMap : public llvm::StringMap<llvm::SmallVector<Entry, 2> > {
John Thompson4f8ba652013-03-12 02:07:30 +0000148public:
149 llvm::DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
150
151 void add(const std::string &Name, enum Entry::Kind Kind, Location Loc) {
152 // Record this entity in its header.
153 HeaderEntry HE = { Name, Loc };
154 CurHeaderContents[Loc.File].push_back(HE);
155
156 // Check whether we've seen this entry before.
157 llvm::SmallVector<Entry, 2> &Entries = (*this)[Name];
158 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
159 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
160 return;
161 }
162
163 // We have not seen this entry before; record it.
164 Entry E = { Kind, Loc };
165 Entries.push_back(E);
166 }
167
168 void mergeCurHeaderContents() {
169 for (llvm::DenseMap<const FileEntry *, HeaderContents>::iterator
170 H = CurHeaderContents.begin(), HEnd = CurHeaderContents.end();
171 H != HEnd; ++H) {
172 // Sort contents.
173 std::sort(H->second.begin(), H->second.end());
174
175 // Check whether we've seen this header before.
176 llvm::DenseMap<const FileEntry *, HeaderContents>::iterator KnownH
177 = AllHeaderContents.find(H->first);
178 if (KnownH == AllHeaderContents.end()) {
179 // We haven't seen this header before; record its contents.
180 AllHeaderContents.insert(*H);
181 continue;
182 }
183
184 // If the header contents are the same, we're done.
185 if (H->second == KnownH->second)
186 continue;
187
188 // Determine what changed.
189 std::set_symmetric_difference(H->second.begin(), H->second.end(),
190 KnownH->second.begin(),
191 KnownH->second.end(),
192 std::back_inserter(HeaderContentMismatches[H->first]));
193 }
194
195 CurHeaderContents.clear();
196 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000197private:
198 llvm::DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
199 llvm::DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
John Thompson4f8ba652013-03-12 02:07:30 +0000200};
201
202class CollectEntitiesVisitor
203 : public RecursiveASTVisitor<CollectEntitiesVisitor>
204{
John Thompson4f8ba652013-03-12 02:07:30 +0000205public:
206 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities)
207 : SM(SM), Entities(Entities) { }
208
209 bool TraverseStmt(Stmt *S) { return true; }
210 bool TraverseType(QualType T) { return true; }
211 bool TraverseTypeLoc(TypeLoc TL) { return true; }
212 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
213 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { return true; }
214 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) { return true; }
215 bool TraverseTemplateName(TemplateName Template) { return true; }
216 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
217 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { return true; }
218 bool TraverseTemplateArguments(const TemplateArgument *Args,
219 unsigned NumArgs) { return true; }
220 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
221 bool TraverseLambdaCapture(LambdaExpr::Capture C) { return true; }
222
223 bool VisitNamedDecl(NamedDecl *ND) {
224 // We only care about file-context variables.
225 if (!ND->getDeclContext()->isFileContext())
226 return true;
227
228 // Skip declarations that tend to be properly multiply-declared.
229 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
230 isa<NamespaceAliasDecl>(ND) ||
231 isa<ClassTemplateSpecializationDecl>(ND) ||
232 isa<UsingDecl>(ND) || isa<UsingShadowDecl>(ND) ||
233 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
234 (isa<TagDecl>(ND) &&
235 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
236 return true;
237
238 std::string Name = ND->getNameAsString();
239 if (Name.empty())
240 return true;
241
242 Location Loc(SM, ND->getLocation());
243 if (!Loc)
244 return true;
245
246 Entities.add(Name, isa<TagDecl>(ND)? Entry::Tag : Entry::Value, Loc);
247 return true;
248 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000249private:
250 SourceManager &SM;
251 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000252};
253
254class CollectEntitiesConsumer : public ASTConsumer {
John Thompson4f8ba652013-03-12 02:07:30 +0000255public:
256 CollectEntitiesConsumer(EntityMap &Entities, Preprocessor &PP)
257 : Entities(Entities), PP(PP) { }
258
259 virtual void HandleTranslationUnit(ASTContext &Ctx) {
260 SourceManager &SM = Ctx.getSourceManager();
261
262 // Collect declared entities.
263 CollectEntitiesVisitor(SM, Entities)
264 .TraverseDecl(Ctx.getTranslationUnitDecl());
265
266 // Collect macro definitions.
267 for (Preprocessor::macro_iterator M = PP.macro_begin(),
268 MEnd = PP.macro_end();
269 M != MEnd; ++M) {
270 Location Loc(SM, M->second->getLocation());
271 if (!Loc)
272 continue;
273
274 Entities.add(M->first->getName().str(), Entry::Macro, Loc);
275 }
276
277 // Merge header contents.
278 Entities.mergeCurHeaderContents();
279 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000280private:
281 EntityMap &Entities;
282 Preprocessor &PP;
John Thompson4f8ba652013-03-12 02:07:30 +0000283};
284
285class CollectEntitiesAction : public SyntaxOnlyAction {
John Thompson1f67ccb2013-03-12 18:51:47 +0000286public:
287 CollectEntitiesAction(EntityMap &Entities) : Entities(Entities) { }
John Thompson4f8ba652013-03-12 02:07:30 +0000288protected:
289 virtual clang::ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
290 StringRef InFile) {
291 return new CollectEntitiesConsumer(Entities, CI.getPreprocessor());
292 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000293private:
294 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000295};
296
297class ModularizeFrontendActionFactory : public FrontendActionFactory {
John Thompson4f8ba652013-03-12 02:07:30 +0000298public:
299 ModularizeFrontendActionFactory(EntityMap &Entities) : Entities(Entities) { }
300
301 virtual CollectEntitiesAction *create() {
302 return new CollectEntitiesAction(Entities);
303 }
John Thompson1f67ccb2013-03-12 18:51:47 +0000304private:
305 EntityMap &Entities;
John Thompson4f8ba652013-03-12 02:07:30 +0000306};
307
308int main(int argc, const char **argv) {
309 // Figure out command-line arguments.
310 if (argc < 2) {
311 llvm::errs() << "Usage: modularize <file containing header names> <arguments>\n";
312 return 1;
313 }
314
315 // Load the list of headers.
316 std::string File = argv[1];
317 llvm::SmallVector<std::string, 8> Headers;
318 {
319 std::ifstream In(File.c_str());
320 if (!In) {
321 llvm::errs() << "Unable to open header list file \"" << File.c_str() << "\"\n";
322 return 2;
323 }
324
325 std::string Line;
326 while (std::getline(In, Line)) {
327 if (Line.empty() || Line[0] == '#')
328 continue;
329
330 Headers.push_back(Line);
331 }
332 }
333
334 // Create the compilation database.
335 llvm::OwningPtr<CompilationDatabase> Compilations;
336 {
337 std::vector<std::string> Arguments;
338 for (int I = 2; I < argc; ++I)
339 Arguments.push_back(argv[I]);
340 SmallString<256> PathBuf;
341 llvm::sys::fs::current_path(PathBuf);
342 Compilations.reset(new FixedCompilationDatabase(Twine(PathBuf), Arguments));
343 }
344
345 // Parse all of the headers, detecting duplicates.
346 EntityMap Entities;
347 ClangTool Tool(*Compilations, Headers);
348 int HadErrors = Tool.run(new ModularizeFrontendActionFactory(Entities));
349
350 // Check for the same entity being defined in multiple places.
351 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
352 E != EEnd; ++E) {
353 Location Tag, Value, Macro;
354 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
355 Location *Which;
356 switch (E->second[I].Kind) {
357 case Entry::Tag: Which = &Tag; break;
358 case Entry::Value: Which = &Value; break;
359 case Entry::Macro: Which = &Macro; break;
360 }
361
362 if (!Which->File) {
363 *Which = E->second[I].Loc;
364 continue;
365 }
366
367 llvm::errs() << "error: '" << E->first().str().c_str()
368 << "' defined at both " << Which->File->getName()
369 << ":" << Which->Line << ":" << Which->Column
370 << " and " << E->second[I].Loc.File->getName() << ":"
371 << E->second[I].Loc.Line << ":" << E->second[I].Loc.Column << "\n";
372 HadErrors = 1;
373 }
374 }
375
376 // Complain about any headers that have contents that differ based on how
377 // they are included.
378 for (llvm::DenseMap<const FileEntry *, HeaderContents>::iterator
379 H = Entities.HeaderContentMismatches.begin(),
380 HEnd = Entities.HeaderContentMismatches.end();
381 H != HEnd; ++H) {
382 if (H->second.empty()) {
383 llvm::errs() << "internal error: phantom header content mismatch\n";
384 continue;
385 }
386
387 HadErrors = 1;
388 llvm::errs() << "error: header '" << H->first->getName()
389 << "' has different contents dependening on how it was included\n";
390 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
391 llvm::errs() << "note: '" << H->second[I].Name.c_str()
392 << "' in " << H->second[I].Loc.File->getName() << " at "
393 << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
394 << " not always provided\n";
395 }
396 }
397
398 return HadErrors;
399}