diff options
author | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-07-18 17:26:57 +0000 |
---|---|---|
committer | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-07-18 17:26:57 +0000 |
commit | 2e97a1e19ef5492a409d206bb544a746cd26360d (patch) | |
tree | a42b9ab74f13ff012147400069076ed5668c9d41 /clang/lib/Tooling/Refactoring/SourceCode.cpp | |
parent | cfa14ac2a7768f90de3532833c88ccffd2ca0078 (diff) | |
download | bcm5719-llvm-2e97a1e19ef5492a409d206bb544a746cd26360d.tar.gz bcm5719-llvm-2e97a1e19ef5492a409d206bb544a746cd26360d.zip |
[LibTooling] Add function to translate and validate source range for editing
Summary:
Adds the function `getRangeForEdit` to validate that a given source range is
editable and, if needed, translate it into a range in the source file (for
example, if it's sourced in macro expansions).
Reviewers: ilya-biryukov
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64924
llvm-svn: 366469
Diffstat (limited to 'clang/lib/Tooling/Refactoring/SourceCode.cpp')
-rw-r--r-- | clang/lib/Tooling/Refactoring/SourceCode.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/clang/lib/Tooling/Refactoring/SourceCode.cpp b/clang/lib/Tooling/Refactoring/SourceCode.cpp index 3a97e178bbd..cee8f43f3e6 100644 --- a/clang/lib/Tooling/Refactoring/SourceCode.cpp +++ b/clang/lib/Tooling/Refactoring/SourceCode.cpp @@ -29,3 +29,37 @@ CharSourceRange clang::tooling::maybeExtendRange(CharSourceRange Range, return Range; return CharSourceRange::getTokenRange(Range.getBegin(), Tok->getLocation()); } + +llvm::Optional<CharSourceRange> +clang::tooling::getRangeForEdit(const CharSourceRange &EditRange, + const SourceManager &SM, + const LangOptions &LangOpts) { + // FIXME: makeFileCharRange() has the disadvantage of stripping off "identity" + // macros. For example, if we're looking to rewrite the int literal 3 to 6, + // and we have the following definition: + // #define DO_NOTHING(x) x + // then + // foo(DO_NOTHING(3)) + // will be rewritten to + // foo(6) + // rather than the arguably better + // foo(DO_NOTHING(6)) + // Decide whether the current behavior is desirable and modify if not. + CharSourceRange Range = Lexer::makeFileCharRange(EditRange, SM, LangOpts); + if (Range.isInvalid()) + return None; + + if (Range.getBegin().isMacroID() || Range.getEnd().isMacroID()) + return None; + if (SM.isInSystemHeader(Range.getBegin()) || + SM.isInSystemHeader(Range.getEnd())) + return None; + + std::pair<FileID, unsigned> BeginInfo = SM.getDecomposedLoc(Range.getBegin()); + std::pair<FileID, unsigned> EndInfo = SM.getDecomposedLoc(Range.getEnd()); + if (BeginInfo.first != EndInfo.first || + BeginInfo.second > EndInfo.second) + return None; + + return Range; +} |