clang-tools  3.8.0
IncludeSorter.h
Go to the documentation of this file.
1 //===------------ IncludeSorter.h - clang-tidy ----------------------------===//
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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
12 
13 #include "../ClangTidy.h"
14 #include <string>
15 
16 namespace clang {
17 namespace tidy {
18 
19 // Class used by IncludeSorterCallback and IncludeInserterCallback to record the
20 // names of the inclusions in a given source file being processed and generate
21 // the necessary commands to sort the inclusions according to the precedence
22 // enocded in IncludeKinds.
24 public:
25  // Supported include styles.
26  enum IncludeStyle { IS_LLVM = 0, IS_Google = 1 };
27 
28  // Converts "llvm" to IS_LLVM, otherwise returns IS_Google.
29  static IncludeStyle parseIncludeStyle(const std::string &Value);
30 
31  // Converts IncludeStyle to string representation.
32  static StringRef toString(IncludeStyle Style);
33 
34  // The classifications of inclusions, in the order they should be sorted.
35  enum IncludeKinds {
36  IK_MainTUInclude = 0, // e.g. #include "foo.h" when editing foo.cc
37  IK_CSystemInclude = 1, // e.g. #include <stdio.h>
38  IK_CXXSystemInclude = 2, // e.g. #include <vector>
39  IK_NonSystemInclude = 3, // e.g. #include "bar.h"
40  IK_InvalidInclude = 4 // total number of valid IncludeKinds
41  };
42 
43  // IncludeSorter constructor; takes the FileID and name of the file to be
44  // processed by the sorter.
45  IncludeSorter(const SourceManager *SourceMgr, const LangOptions *LangOpts,
46  const FileID FileID, StringRef FileName, IncludeStyle Style);
47 
48  // Returns the SourceManager-specific file ID for the file being handled by
49  // the sorter.
50  const FileID current_FileID() const { return CurrentFileID; }
51 
52  // Adds the given #include to the sorter.
53  void AddInclude(StringRef FileName, bool IsAngled,
54  SourceLocation HashLocation, SourceLocation EndLocation);
55 
56  // Returns the edits needed to sort the current set of includes and reset the
57  // internal state (so that different blocks of includes are sorted separately
58  // within the same file).
59  std::vector<FixItHint> GetEdits();
60 
61  // Creates a quoted inclusion directive in the right sort order. Returns None
62  // on error or if header inclusion directive for header already exists.
63  Optional<FixItHint> CreateIncludeInsertion(StringRef FileName, bool IsAngled);
64 
65 private:
66  typedef SmallVector<SourceRange, 1> SourceRangeVector;
67 
68  // Creates a fix-it for the given replacements.
69  // Takes the the source location that will be replaced, and the new text.
70  FixItHint CreateFixIt(SourceRange EditRange, const std::string &NewText);
71 
72  const SourceManager *SourceMgr;
73  const LangOptions *LangOpts;
74  const IncludeStyle Style;
75  FileID CurrentFileID;
76  // The file name stripped of common suffixes.
77  StringRef CanonicalFile;
78  // Locations of visited include directives.
79  SourceRangeVector SourceLocations;
80  // Mapping from file name to #include locations.
81  llvm::StringMap<SourceRangeVector> IncludeLocations;
82  // Includes sorted into buckets.
83  SmallVector<std::string, 1> IncludeBucket[IK_InvalidInclude];
84 };
85 
86 } // namespace tidy
87 } // namespace clang
88 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
Optional< FixItHint > CreateIncludeInsertion(StringRef FileName, bool IsAngled)
static IncludeStyle parseIncludeStyle(const std::string &Value)
const FileID current_FileID() const
Definition: IncludeSorter.h:50
void AddInclude(StringRef FileName, bool IsAngled, SourceLocation HashLocation, SourceLocation EndLocation)
IncludeSorter(const SourceManager *SourceMgr, const LangOptions *LangOpts, const FileID FileID, StringRef FileName, IncludeStyle Style)
static StringRef toString(IncludeStyle Style)
std::vector< FixItHint > GetEdits()
bool IsAngled
true if this was an include with angle brackets