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