2015-08-11 19:37:48 +08:00
|
|
|
//===------------ IncludeSorter.h - clang-tidy ----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
|
|
|
|
|
2015-08-14 22:31:31 +08:00
|
|
|
#include "../ClangTidy.h"
|
2015-08-11 19:37:48 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2016-05-16 22:34:20 +08:00
|
|
|
namespace utils {
|
2015-08-11 19:37:48 +08:00
|
|
|
|
|
|
|
// Class used by IncludeSorterCallback and IncludeInserterCallback to record the
|
|
|
|
// names of the inclusions in a given source file being processed and generate
|
|
|
|
// the necessary commands to sort the inclusions according to the precedence
|
|
|
|
// enocded in IncludeKinds.
|
|
|
|
class IncludeSorter {
|
2015-08-14 20:33:25 +08:00
|
|
|
public:
|
2015-08-11 19:37:48 +08:00
|
|
|
// Supported include styles.
|
2015-08-14 20:33:25 +08:00
|
|
|
enum IncludeStyle { IS_LLVM = 0, IS_Google = 1 };
|
2015-08-11 19:37:48 +08:00
|
|
|
|
2015-10-07 00:27:03 +08:00
|
|
|
// Converts "llvm" to IS_LLVM, otherwise returns IS_Google.
|
|
|
|
static IncludeStyle parseIncludeStyle(const std::string &Value);
|
|
|
|
|
|
|
|
// Converts IncludeStyle to string representation.
|
|
|
|
static StringRef toString(IncludeStyle Style);
|
|
|
|
|
2015-08-11 19:37:48 +08:00
|
|
|
// The classifications of inclusions, in the order they should be sorted.
|
|
|
|
enum IncludeKinds {
|
2015-08-14 20:33:25 +08:00
|
|
|
IK_MainTUInclude = 0, // e.g. #include "foo.h" when editing foo.cc
|
|
|
|
IK_CSystemInclude = 1, // e.g. #include <stdio.h>
|
|
|
|
IK_CXXSystemInclude = 2, // e.g. #include <vector>
|
|
|
|
IK_NonSystemInclude = 3, // e.g. #include "bar.h"
|
|
|
|
IK_InvalidInclude = 4 // total number of valid IncludeKinds
|
2015-08-11 19:37:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// IncludeSorter constructor; takes the FileID and name of the file to be
|
|
|
|
// processed by the sorter.
|
2015-08-14 20:33:25 +08:00
|
|
|
IncludeSorter(const SourceManager *SourceMgr, const LangOptions *LangOpts,
|
|
|
|
const FileID FileID, StringRef FileName, IncludeStyle Style);
|
2015-08-11 19:37:48 +08:00
|
|
|
|
|
|
|
// Returns the SourceManager-specific file ID for the file being handled by
|
|
|
|
// the sorter.
|
|
|
|
const FileID current_FileID() const { return CurrentFileID; }
|
|
|
|
|
|
|
|
// Adds the given #include to the sorter.
|
|
|
|
void AddInclude(StringRef FileName, bool IsAngled,
|
|
|
|
SourceLocation HashLocation, SourceLocation EndLocation);
|
|
|
|
|
|
|
|
// Returns the edits needed to sort the current set of includes and reset the
|
|
|
|
// internal state (so that different blocks of includes are sorted separately
|
|
|
|
// within the same file).
|
|
|
|
std::vector<FixItHint> GetEdits();
|
|
|
|
|
|
|
|
// Creates a quoted inclusion directive in the right sort order. Returns None
|
|
|
|
// on error or if header inclusion directive for header already exists.
|
2015-08-14 20:33:25 +08:00
|
|
|
Optional<FixItHint> CreateIncludeInsertion(StringRef FileName, bool IsAngled);
|
2015-08-11 19:37:48 +08:00
|
|
|
|
2015-08-14 20:33:25 +08:00
|
|
|
private:
|
2015-08-11 19:37:48 +08:00
|
|
|
typedef SmallVector<SourceRange, 1> SourceRangeVector;
|
|
|
|
|
2015-08-14 20:33:25 +08:00
|
|
|
const SourceManager *SourceMgr;
|
|
|
|
const LangOptions *LangOpts;
|
2015-08-11 19:37:48 +08:00
|
|
|
const IncludeStyle Style;
|
|
|
|
FileID CurrentFileID;
|
|
|
|
// The file name stripped of common suffixes.
|
|
|
|
StringRef CanonicalFile;
|
|
|
|
// Locations of visited include directives.
|
|
|
|
SourceRangeVector SourceLocations;
|
|
|
|
// Mapping from file name to #include locations.
|
|
|
|
llvm::StringMap<SourceRangeVector> IncludeLocations;
|
|
|
|
// Includes sorted into buckets.
|
|
|
|
SmallVector<std::string, 1> IncludeBucket[IK_InvalidInclude];
|
|
|
|
};
|
|
|
|
|
2016-05-03 10:54:05 +08:00
|
|
|
} // namespace utils
|
2015-08-14 20:33:25 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
2015-08-11 19:37:48 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDESORTER_H
|