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