clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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.
23 class IncludeSorter {
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
clang::tidy::IncludeSorter::CreateIncludeInsertion
Optional< FixItHint > CreateIncludeInsertion(StringRef FileName, bool IsAngled)
Definition: IncludeSorter.cpp:112
clang::tidy::IncludeSorter::IncludeStyle
IncludeStyle
Definition: IncludeSorter.h:26
clang::tidy::IncludeSorter::parseIncludeStyle
static IncludeStyle parseIncludeStyle(const std::string &Value)
Definition: IncludeSorter.cpp:286
clang::tidy::IncludeSorter::current_FileID
const FileID current_FileID() const
Definition: IncludeSorter.h:50
clang::tidy::IncludeSorter::IK_CXXSystemInclude
Definition: IncludeSorter.h:38
clang::tidy::IncludeSorter::AddInclude
void AddInclude(StringRef FileName, bool IsAngled, SourceLocation HashLocation, SourceLocation EndLocation)
Definition: IncludeSorter.cpp:91
clang::tidy::IncludeSorter::IncludeSorter
IncludeSorter(const SourceManager *SourceMgr, const LangOptions *LangOpts, const FileID FileID, StringRef FileName, IncludeStyle Style)
Definition: IncludeSorter.cpp:84
clang::tidy::IncludeSorter::toString
static StringRef toString(IncludeStyle Style)
Definition: IncludeSorter.cpp:290
clang::tidy::IncludeSorter::IS_Google
Definition: IncludeSorter.h:26
clang::tidy::IncludeSorter::GetEdits
std::vector< FixItHint > GetEdits()
Definition: IncludeSorter.cpp:177
clang::tidy::IncludeSorter::IK_InvalidInclude
Definition: IncludeSorter.h:40
clang::tidy::IncludeSorter::IK_CSystemInclude
Definition: IncludeSorter.h:37
clang::tidy::IncludeSorter::IK_MainTUInclude
Definition: IncludeSorter.h:36
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:37
clang::tidy::IncludeSorter::IncludeKinds
IncludeKinds
Definition: IncludeSorter.h:35
clang::tidy::IncludeSorter
Definition: IncludeSorter.h:23
clang::tidy::IncludeSorter::IS_LLVM
Definition: IncludeSorter.h:26
clang::tidy::IncludeSorter::IK_NonSystemInclude
Definition: IncludeSorter.h:39