clang-tools 20.0.0git
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 "../ClangTidyCheck.h"
13#include <optional>
14#include <string>
15
16namespace clang::tidy {
17namespace 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``.
24public:
25 /// Supported include styles.
27
28 /// The classifications of inclusions, in the order they should be sorted.
30 IK_MainTUInclude = 0, ///< e.g. ``#include "foo.h"`` when editing foo.cc
31 IK_CSystemInclude = 1, ///< e.g. ``#include <stdio.h>``
32 IK_CXXSystemInclude = 2, ///< e.g. ``#include <vector>``
33 IK_NonSystemInclude = 3, ///< e.g. ``#include "bar.h"``
34 IK_GeneratedInclude = 4, ///< e.g. ``#include "bar.proto.h"``
35 IK_InvalidInclude = 5 ///< total number of valid ``IncludeKind``s
36 };
37
38 /// ``IncludeSorter`` constructor; takes the FileID and name of the file to be
39 /// processed by the sorter.
40 IncludeSorter(const SourceManager *SourceMgr, const FileID FileID,
41 StringRef FileName, IncludeStyle Style);
42
43 /// Adds the given include directive to the sorter.
44 void addInclude(StringRef FileName, bool IsAngled,
45 SourceLocation HashLocation, SourceLocation EndLocation);
46
47 /// Creates a quoted inclusion directive in the right sort order. Returns
48 /// std::nullopt on error or if header inclusion directive for header already
49 /// exists.
50 std::optional<FixItHint> createIncludeInsertion(StringRef FileName,
51 bool IsAngled);
52
53private:
54 using SourceRangeVector = SmallVector<SourceRange, 1>;
55
56 const SourceManager *SourceMgr;
57 const IncludeStyle Style;
58 FileID CurrentFileID;
59 /// The file name stripped of common suffixes.
60 StringRef CanonicalFile;
61 /// Locations of visited include directives.
62 SourceRangeVector SourceLocations;
63 /// Mapping from file name to #include locations.
64 llvm::StringMap<SourceRangeVector> IncludeLocations;
65 /// Includes sorted into buckets.
66 SmallVector<std::string, 1> IncludeBucket[IK_InvalidInclude];
67};
68
69} // namespace utils
70
71template <> struct OptionEnumMapping<utils::IncludeSorter::IncludeStyle> {
72 static ArrayRef<std::pair<utils::IncludeSorter::IncludeStyle, StringRef>>
74};
75} // namespace clang::tidy
76#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
bool IsAngled
true if this was an include with angle brackets
StringRef FileName
Class used by IncludeInserterCallback to record the names of the inclusions in a given source file be...
Definition: IncludeSorter.h:23
IncludeKinds
The classifications of inclusions, in the order they should be sorted.
Definition: IncludeSorter.h:29
@ IK_CSystemInclude
e.g. #include <stdio.h>
Definition: IncludeSorter.h:31
@ IK_NonSystemInclude
e.g. #include "bar.h"
Definition: IncludeSorter.h:33
@ IK_InvalidInclude
total number of valid IncludeKinds
Definition: IncludeSorter.h:35
@ IK_MainTUInclude
e.g. #include "foo.h" when editing foo.cc
Definition: IncludeSorter.h:30
@ IK_GeneratedInclude
e.g. #include "bar.proto.h"
Definition: IncludeSorter.h:34
@ IK_CXXSystemInclude
e.g. #include <vector>
Definition: IncludeSorter.h:32
void addInclude(StringRef FileName, bool IsAngled, SourceLocation HashLocation, SourceLocation EndLocation)
Adds the given include directive to the sorter.
IncludeStyle
Supported include styles.
Definition: IncludeSorter.h:26
std::optional< FixItHint > createIncludeInsertion(StringRef FileName, bool IsAngled)
Creates a quoted inclusion directive in the right sort order.
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...
static ArrayRef< std::pair< T, StringRef > > getEnumMapping()=delete