Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:22

0001 //===------------ IncludeSorter.h - clang-tidy ----------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 #include <optional>
0014 #include <string>
0015 
0016 namespace clang::tidy {
0017 namespace utils {
0018 
0019 /// Class used by ``IncludeInserterCallback`` to record the names of the
0020 /// inclusions in a given source file being processed and generate the necessary
0021 /// commands to sort the inclusions according to the precedence encoded in
0022 /// ``IncludeKinds``.
0023 class IncludeSorter {
0024 public:
0025   /// Supported include styles.
0026   enum IncludeStyle { IS_LLVM = 0, IS_Google = 1, IS_Google_ObjC };
0027 
0028   /// The classifications of inclusions, in the order they should be sorted.
0029   enum IncludeKinds {
0030     IK_MainTUInclude = 0,    ///< e.g. ``#include "foo.h"`` when editing foo.cc
0031     IK_CSystemInclude = 1,   ///< e.g. ``#include <stdio.h>``
0032     IK_CXXSystemInclude = 2, ///< e.g. ``#include <vector>``
0033     IK_NonSystemInclude = 3, ///< e.g. ``#include "bar.h"``
0034     IK_GeneratedInclude = 4, ///< e.g. ``#include "bar.proto.h"``
0035     IK_InvalidInclude = 5    ///< total number of valid ``IncludeKind``s
0036   };
0037 
0038   /// ``IncludeSorter`` constructor; takes the FileID and name of the file to be
0039   /// processed by the sorter.
0040   IncludeSorter(const SourceManager *SourceMgr, const FileID FileID,
0041                 StringRef FileName, IncludeStyle Style);
0042 
0043   /// Adds the given include directive to the sorter.
0044   void addInclude(StringRef FileName, bool IsAngled,
0045                   SourceLocation HashLocation, SourceLocation EndLocation);
0046 
0047   /// Creates a quoted inclusion directive in the right sort order. Returns
0048   /// std::nullopt on error or if header inclusion directive for header already
0049   /// exists.
0050   std::optional<FixItHint> createIncludeInsertion(StringRef FileName,
0051                                                   bool IsAngled);
0052 
0053 private:
0054   using SourceRangeVector = SmallVector<SourceRange, 1>;
0055 
0056   const SourceManager *SourceMgr;
0057   const IncludeStyle Style;
0058   FileID CurrentFileID;
0059   /// The file name stripped of common suffixes.
0060   StringRef CanonicalFile;
0061   /// Locations of visited include directives.
0062   SourceRangeVector SourceLocations;
0063   /// Mapping from file name to #include locations.
0064   llvm::StringMap<SourceRangeVector> IncludeLocations;
0065   /// Includes sorted into buckets.
0066   SmallVector<std::string, 1> IncludeBucket[IK_InvalidInclude];
0067 };
0068 
0069 } // namespace utils
0070 
0071 template <> struct OptionEnumMapping<utils::IncludeSorter::IncludeStyle> {
0072   static ArrayRef<std::pair<utils::IncludeSorter::IncludeStyle, StringRef>>
0073   getEnumMapping();
0074 };
0075 } // namespace clang::tidy
0076 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H