clang-tools  9.0.0svn
IncludeOrderCheck.cpp
Go to the documentation of this file.
1 //===--- IncludeOrderCheck.cpp - clang-tidy -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "IncludeOrderCheck.h"
10 #include "clang/Frontend/CompilerInstance.h"
11 #include "clang/Lex/PPCallbacks.h"
12 #include "clang/Lex/Preprocessor.h"
13 
14 #include <map>
15 
16 namespace clang {
17 namespace tidy {
18 namespace llvm {
19 
20 namespace {
21 class IncludeOrderPPCallbacks : public PPCallbacks {
22 public:
23  explicit IncludeOrderPPCallbacks(ClangTidyCheck &Check, SourceManager &SM)
24  : LookForMainModule(true), Check(Check), SM(SM) {}
25 
26  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
27  StringRef FileName, bool IsAngled,
28  CharSourceRange FilenameRange, const FileEntry *File,
29  StringRef SearchPath, StringRef RelativePath,
30  const Module *Imported,
31  SrcMgr::CharacteristicKind FileType) override;
32  void EndOfMainFile() override;
33 
34 private:
35  struct IncludeDirective {
36  SourceLocation Loc; ///< '#' location in the include directive
37  CharSourceRange Range; ///< SourceRange for the file name
38  std::string Filename; ///< Filename as a string
39  bool IsAngled; ///< true if this was an include with angle brackets
40  bool IsMainModule; ///< true if this was the first include in a file
41  };
42 
43  typedef std::vector<IncludeDirective> FileIncludes;
44  std::map<clang::FileID, FileIncludes> IncludeDirectives;
45  bool LookForMainModule;
46 
47  ClangTidyCheck &Check;
48  SourceManager &SM;
49 };
50 } // namespace
51 
52 void IncludeOrderCheck::registerPPCallbacks(CompilerInstance &Compiler) {
53  Compiler.getPreprocessor().addPPCallbacks(
54  ::llvm::make_unique<IncludeOrderPPCallbacks>(
55  *this, Compiler.getSourceManager()));
56 }
57 
58 static int getPriority(StringRef Filename, bool IsAngled, bool IsMainModule) {
59  // We leave the main module header at the top.
60  if (IsMainModule)
61  return 0;
62 
63  // LLVM and clang headers are in the penultimate position.
64  if (Filename.startswith("llvm/") || Filename.startswith("llvm-c/") ||
65  Filename.startswith("clang/") || Filename.startswith("clang-c/"))
66  return 2;
67 
68  // System headers are sorted to the end.
69  if (IsAngled || Filename.startswith("gtest/"))
70  return 3;
71 
72  // Other headers are inserted between the main module header and LLVM headers.
73  return 1;
74 }
75 
76 void IncludeOrderPPCallbacks::InclusionDirective(
77  SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
78  bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
79  StringRef SearchPath, StringRef RelativePath, const Module *Imported,
80  SrcMgr::CharacteristicKind FileType) {
81  // We recognize the first include as a special main module header and want
82  // to leave it in the top position.
83  IncludeDirective ID = {HashLoc, FilenameRange, FileName, IsAngled, false};
84  if (LookForMainModule && !IsAngled) {
85  ID.IsMainModule = true;
86  LookForMainModule = false;
87  }
88 
89  // Bucket the include directives by the id of the file they were declared in.
90  IncludeDirectives[SM.getFileID(HashLoc)].push_back(std::move(ID));
91 }
92 
93 void IncludeOrderPPCallbacks::EndOfMainFile() {
94  LookForMainModule = true;
95  if (IncludeDirectives.empty())
96  return;
97 
98  // TODO: find duplicated includes.
99 
100  // Form blocks of includes. We don't want to sort across blocks. This also
101  // implicitly makes us never reorder over #defines or #if directives.
102  // FIXME: We should be more careful about sorting below comments as we don't
103  // know if the comment refers to the next include or the whole block that
104  // follows.
105  for (auto &Bucket : IncludeDirectives) {
106  auto &FileDirectives = Bucket.second;
107  std::vector<unsigned> Blocks(1, 0);
108  for (unsigned I = 1, E = FileDirectives.size(); I != E; ++I)
109  if (SM.getExpansionLineNumber(FileDirectives[I].Loc) !=
110  SM.getExpansionLineNumber(FileDirectives[I - 1].Loc) + 1)
111  Blocks.push_back(I);
112  Blocks.push_back(FileDirectives.size()); // Sentinel value.
113 
114  // Get a vector of indices.
115  std::vector<unsigned> IncludeIndices;
116  for (unsigned I = 0, E = FileDirectives.size(); I != E; ++I)
117  IncludeIndices.push_back(I);
118 
119  // Sort the includes. We first sort by priority, then lexicographically.
120  for (unsigned BI = 0, BE = Blocks.size() - 1; BI != BE; ++BI)
121  std::sort(IncludeIndices.begin() + Blocks[BI],
122  IncludeIndices.begin() + Blocks[BI + 1],
123  [&FileDirectives](unsigned LHSI, unsigned RHSI) {
124  IncludeDirective &LHS = FileDirectives[LHSI];
125  IncludeDirective &RHS = FileDirectives[RHSI];
126 
127  int PriorityLHS =
128  getPriority(LHS.Filename, LHS.IsAngled, LHS.IsMainModule);
129  int PriorityRHS =
130  getPriority(RHS.Filename, RHS.IsAngled, RHS.IsMainModule);
131 
132  return std::tie(PriorityLHS, LHS.Filename) <
133  std::tie(PriorityRHS, RHS.Filename);
134  });
135 
136  // Emit a warning for each block and fixits for all changes within that
137  // block.
138  for (unsigned BI = 0, BE = Blocks.size() - 1; BI != BE; ++BI) {
139  // Find the first include that's not in the right position.
140  unsigned I, E;
141  for (I = Blocks[BI], E = Blocks[BI + 1]; I != E; ++I)
142  if (IncludeIndices[I] != I)
143  break;
144 
145  if (I == E)
146  continue;
147 
148  // Emit a warning.
149  auto D = Check.diag(FileDirectives[I].Loc,
150  "#includes are not sorted properly");
151 
152  // Emit fix-its for all following includes in this block.
153  for (; I != E; ++I) {
154  if (IncludeIndices[I] == I)
155  continue;
156  const IncludeDirective &CopyFrom = FileDirectives[IncludeIndices[I]];
157 
158  SourceLocation FromLoc = CopyFrom.Range.getBegin();
159  const char *FromData = SM.getCharacterData(FromLoc);
160  unsigned FromLen = std::strcspn(FromData, "\n");
161 
162  StringRef FixedName(FromData, FromLen);
163 
164  SourceLocation ToLoc = FileDirectives[I].Range.getBegin();
165  const char *ToData = SM.getCharacterData(ToLoc);
166  unsigned ToLen = std::strcspn(ToData, "\n");
167  auto ToRange =
168  CharSourceRange::getCharRange(ToLoc, ToLoc.getLocWithOffset(ToLen));
169 
170  D << FixItHint::CreateReplacement(ToRange, FixedName);
171  }
172  }
173  }
174 
175  IncludeDirectives.clear();
176 }
177 
178 } // namespace llvm
179 } // namespace tidy
180 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
Some operations such as code completion produce a set of candidates.
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
std::string Filename
Filename as a string.
bool IsAngled
true if this was an include with angle brackets
PathRef FileName
bool IsMainModule
true if this was the first include in a file
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
static int getPriority(StringRef Filename, bool IsAngled, bool IsMainModule)