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
|
|
|
|
2016-06-17 20:01:15 +08:00
|
|
|
/// Class used by ``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 encoded in
|
|
|
|
/// ``IncludeKinds``.
|
2015-08-11 19:37:48 +08:00
|
|
|
class IncludeSorter {
|
2015-08-14 20:33:25 +08:00
|
|
|
public:
|
2016-06-17 20:01:15 +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
|
|
|
|
2016-06-17 20:01:15 +08:00
|
|
|
/// Converts "llvm" to ``IS_LLVM``, otherwise returns ``IS_Google``.
|
2015-10-07 00:27:03 +08:00
|
|
|
static IncludeStyle parseIncludeStyle(const std::string &Value);
|
|
|
|
|
2016-06-17 20:01:15 +08:00
|
|
|
/// Converts ``IncludeStyle`` to string representation.
|
2015-10-07 00:27:03 +08:00
|
|
|
static StringRef toString(IncludeStyle Style);
|
|
|
|
|
2016-06-17 20:01:15 +08:00
|
|
|
/// The classifications of inclusions, in the order they should be sorted.
|
2015-08-11 19:37:48 +08:00
|
|
|
enum IncludeKinds {
|
2016-06-17 20:01:15 +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 ``IncludeKind``s
|
2015-08-11 19:37:48 +08:00
|
|
|
};
|
|
|
|
|
2016-06-17 20:01:15 +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
|
|
|
|
2016-06-17 20:01:15 +08:00
|
|
|
/// Returns the ``SourceManager``-specific file ID for the file being handled
|
|
|
|
/// by the sorter.
|
2015-08-11 19:37:48 +08:00
|
|
|
const FileID current_FileID() const { return CurrentFileID; }
|
|
|
|
|
2016-06-17 20:01:15 +08:00
|
|
|
/// Adds the given include directive to the sorter.
|
2015-08-11 19:37:48 +08:00
|
|
|
void AddInclude(StringRef FileName, bool IsAngled,
|
|
|
|
SourceLocation HashLocation, SourceLocation EndLocation);
|
|
|
|
|
2016-06-17 20:01:15 +08:00
|
|
|
/// 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).
|
2015-08-11 19:37:48 +08:00
|
|
|
std::vector<FixItHint> GetEdits();
|
|
|
|
|
2016-06-17 20:01:15 +08:00
|
|
|
/// 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;
|
2016-06-17 20:01:15 +08:00
|
|
|
/// The file name stripped of common suffixes.
|
2015-08-11 19:37:48 +08:00
|
|
|
StringRef CanonicalFile;
|
2016-06-17 20:01:15 +08:00
|
|
|
/// Locations of visited include directives.
|
2015-08-11 19:37:48 +08:00
|
|
|
SourceRangeVector SourceLocations;
|
2016-06-17 20:01:15 +08:00
|
|
|
/// Mapping from file name to #include locations.
|
2015-08-11 19:37:48 +08:00
|
|
|
llvm::StringMap<SourceRangeVector> IncludeLocations;
|
2016-06-17 20:01:15 +08:00
|
|
|
/// Includes sorted into buckets.
|
2015-08-11 19:37:48 +08:00
|
|
|
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
|