Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[find-all-symbols] Add IWYU private pragma support.
Reviewers: djasper, klimek Subscribers: kimgr, cfe-commits, bkramer, ioeric Differential Revision: http://reviews.llvm.org/D19816 llvm-svn: 269779
- Loading branch information
Showing
8 changed files
with
226 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
clang-tools-extra/include-fixer/find-all-symbols/HeaderMapCollector.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//===-- HeaderMapCoolector.h - find all symbols------------------*- C++ -*-===// | ||
// | ||
// 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_FIND_ALL_SYMBOLS_HEADER_MAP_COLLECTOR_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_HEADER_MAP_COLLECTOR_H | ||
|
||
#include "llvm/ADT/StringMap.h" | ||
#include <string> | ||
|
||
namespace clang { | ||
namespace find_all_symbols { | ||
|
||
/// \brief HeaderMappCollector collects all remapping header files. | ||
class HeaderMapCollector { | ||
public: | ||
typedef llvm::StringMap<std::string> HeaderMap; | ||
|
||
void addHeaderMapping(llvm::StringRef OrignalHeaderPath, | ||
llvm::StringRef MappingHeaderPath) { | ||
HeaderMappingTable[OrignalHeaderPath] = MappingHeaderPath; | ||
}; | ||
const HeaderMap &getHeaderMappingTable() { return HeaderMappingTable; }; | ||
|
||
private: | ||
/// A string-to-string map saving the mapping relationship. | ||
HeaderMap HeaderMappingTable; | ||
}; | ||
|
||
} // namespace find_all_symbols | ||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_HEADER_MAP_COLLECTOR_H |
37 changes: 37 additions & 0 deletions
37
clang-tools-extra/include-fixer/find-all-symbols/PragmaCommentHandler.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===-- PragmaCommentHandler.cpp - find all symbols -----------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "PragmaCommentHandler.h" | ||
#include "FindAllSymbols.h" | ||
#include "HeaderMapCollector.h" | ||
#include "clang/Lex/Preprocessor.h" | ||
#include "llvm/Support/Regex.h" | ||
|
||
namespace clang { | ||
namespace find_all_symbols { | ||
namespace { | ||
const char IWYUPragma[] = "// IWYU pragma: private, include "; | ||
} // namespace | ||
|
||
bool PragmaCommentHandler::HandleComment(Preprocessor &PP, SourceRange Range) { | ||
StringRef Text = | ||
Lexer::getSourceText(CharSourceRange::getCharRange(Range), | ||
PP.getSourceManager(), PP.getLangOpts()); | ||
size_t Pos = Text.find(IWYUPragma); | ||
if (Pos == StringRef::npos) | ||
return false; | ||
StringRef RemappingFilePath = Text.substr(Pos + std::strlen(IWYUPragma)); | ||
Collector->addHeaderMapping( | ||
PP.getSourceManager().getFilename(Range.getBegin()), | ||
RemappingFilePath.trim("\"<>")); | ||
return false; | ||
} | ||
|
||
} // namespace find_all_symbols | ||
} // namespace clang |
41 changes: 41 additions & 0 deletions
41
clang-tools-extra/include-fixer/find-all-symbols/PragmaCommentHandler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//===-- PragmaCommentHandler.h - find all symbols----------------*- C++ -*-===// | ||
// | ||
// 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_FIND_ALL_SYMBOLS_PRAGMA_COMMENT_HANDLER_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_PRAGMA_COMMENT_HANDLER_H | ||
|
||
#include "clang/Basic/SourceLocation.h" | ||
#include "clang/Lex/Preprocessor.h" | ||
#include <map> | ||
|
||
namespace clang { | ||
namespace find_all_symbols { | ||
|
||
class HeaderMapCollector; | ||
|
||
/// \brief PragmaCommentHandler parses pragma comment on include files to | ||
/// determine when we should include a different header from the header that | ||
/// directly defines a symbol. | ||
/// | ||
/// Currently it only supports IWYU private pragma: | ||
/// https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUPragmas.md#iwyu-pragma-private | ||
class PragmaCommentHandler : public clang::CommentHandler { | ||
public: | ||
PragmaCommentHandler(HeaderMapCollector *Collector) : Collector(Collector) {} | ||
|
||
bool HandleComment(Preprocessor &PP, SourceRange Range) override; | ||
|
||
private: | ||
HeaderMapCollector *const Collector; | ||
}; | ||
|
||
} // namespace find_all_symbols | ||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_PRAGMA_COMMENT_HANDLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters