clang-tools 10.0.0

IncludeSorter.h
Go to the documentation of this file.
1 //===------------ IncludeSorter.h - 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
11
12 #include "../ClangTidy.h"
13 #include <string>
14
15 namespace clang {
16 namespace tidy {
17 namespace utils {
18
19 /// Class used by ``IncludeInserterCallback`` to record the names of the
20 /// inclusions in a given source file being processed and generate the necessary
21 /// commands to sort the inclusions according to the precedence encoded in
22 /// ``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 ``IncludeKind``s
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
49 /// by the sorter.
50 const FileID current_FileID() const { return CurrentFileID; }
51
52 /// Adds the given include directive 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 const SourceManager *SourceMgr;
69 const LangOptions *LangOpts;
70 const IncludeStyle Style;
71 FileID CurrentFileID;
72 /// The file name stripped of common suffixes.
73 StringRef CanonicalFile;
74 /// Locations of visited include directives.
75 SourceRangeVector SourceLocations;
76 /// Mapping from file name to #include locations.
77 llvm::StringMap<SourceRangeVector> IncludeLocations;
78 /// Includes sorted into buckets.
79 SmallVector<std::string, 1> IncludeBucket[IK_InvalidInclude];
80 };
81
82 } // namespace utils
83 } // namespace tidy
84 } // namespace clang
85 #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:36
clang::tidy::utils::IncludeSorter::toString
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
Definition: IncludeSorter.cpp:283
clang::tidy::utils::IncludeSorter::IncludeStyle
IncludeStyle
Supported include styles.
Definition: IncludeSorter.h:26
clang::tidy::utils::IncludeSorter::IS_LLVM
Definition: IncludeSorter.h:26
clang::tidy::utils::IncludeSorter::IncludeKinds
IncludeKinds
The classifications of inclusions, in the order they should be sorted.
Definition: IncludeSorter.h:35
clang::tidy::utils::IncludeSorter::IK_CXXSystemInclude
e.g. #include <vector>
Definition: IncludeSorter.h:38
clang::tidy::utils::IncludeSorter::IK_CSystemInclude
e.g. #include <stdio.h>
Definition: IncludeSorter.h:37
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:50
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:85
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:40
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:92
clang::tidy::utils::IncludeSorter::IK_InvalidInclude
total number of valid IncludeKinds
Definition: IncludeSorter.h:40
clang::tidy::utils::IncludeSorter::IK_NonSystemInclude
e.g. #include "bar.h"
Definition: IncludeSorter.h:39
FileName
PathRef FileName
Definition: CodeComplete.cpp:1018
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::IncludeSorter::parseIncludeStyle
static IncludeStyle parseIncludeStyle(const std::string &Value)
Converts "llvm" to IS_LLVM, otherwise returns IS_Google.
Definition: IncludeSorter.cpp:279
clang::tidy::utils::IncludeSorter::IS_Google
Definition: IncludeSorter.h:26
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:113
clang::tidy::utils::IncludeSorter
Class used by IncludeInserterCallback to record the names of the inclusions in a given source file be...
Definition: IncludeSorter.h:23
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:178