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