Mercurial > hg > CbC > CbC_llvm
comparison clang-tools-extra/clangd/SemanticSelection.cpp @ 252:1f2b6ac9f198 llvm-original
LLVM16-1
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 18 Aug 2023 09:04:13 +0900 |
parents | c4bab56944e8 |
children |
comparison
equal
deleted
inserted
replaced
237:c80f45b162ad | 252:1f2b6ac9f198 |
---|---|
23 #include "clang/Tooling/Syntax/Tree.h" | 23 #include "clang/Tooling/Syntax/Tree.h" |
24 #include "llvm/ADT/ArrayRef.h" | 24 #include "llvm/ADT/ArrayRef.h" |
25 #include "llvm/ADT/StringRef.h" | 25 #include "llvm/ADT/StringRef.h" |
26 #include "llvm/Support/Casting.h" | 26 #include "llvm/Support/Casting.h" |
27 #include "llvm/Support/Error.h" | 27 #include "llvm/Support/Error.h" |
28 #include <optional> | |
28 #include <queue> | 29 #include <queue> |
29 #include <vector> | 30 #include <vector> |
30 | 31 |
31 namespace clang { | 32 namespace clang { |
32 namespace clangd { | 33 namespace clangd { |
38 if (Result.empty() || Result.back() != R) { | 39 if (Result.empty() || Result.back() != R) { |
39 Result.push_back(R); | 40 Result.push_back(R); |
40 } | 41 } |
41 } | 42 } |
42 | 43 |
43 llvm::Optional<FoldingRange> toFoldingRange(SourceRange SR, | 44 std::optional<FoldingRange> toFoldingRange(SourceRange SR, |
44 const SourceManager &SM) { | 45 const SourceManager &SM) { |
45 const auto Begin = SM.getDecomposedLoc(SR.getBegin()), | 46 const auto Begin = SM.getDecomposedLoc(SR.getBegin()), |
46 End = SM.getDecomposedLoc(SR.getEnd()); | 47 End = SM.getDecomposedLoc(SR.getEnd()); |
47 // Do not produce folding ranges if either range ends is not within the main | 48 // Do not produce folding ranges if either range ends is not within the main |
48 // file. Macros have their own FileID so this also checks if locations are not | 49 // file. Macros have their own FileID so this also checks if locations are not |
49 // within the macros. | 50 // within the macros. |
50 if ((Begin.first != SM.getMainFileID()) || (End.first != SM.getMainFileID())) | 51 if ((Begin.first != SM.getMainFileID()) || (End.first != SM.getMainFileID())) |
51 return llvm::None; | 52 return std::nullopt; |
52 FoldingRange Range; | 53 FoldingRange Range; |
53 Range.startCharacter = SM.getColumnNumber(Begin.first, Begin.second) - 1; | 54 Range.startCharacter = SM.getColumnNumber(Begin.first, Begin.second) - 1; |
54 Range.startLine = SM.getLineNumber(Begin.first, Begin.second) - 1; | 55 Range.startLine = SM.getLineNumber(Begin.first, Begin.second) - 1; |
55 Range.endCharacter = SM.getColumnNumber(End.first, End.second) - 1; | 56 Range.endCharacter = SM.getColumnNumber(End.first, End.second) - 1; |
56 Range.endLine = SM.getLineNumber(End.first, End.second) - 1; | 57 Range.endLine = SM.getLineNumber(End.first, End.second) - 1; |
57 return Range; | 58 return Range; |
58 } | 59 } |
59 | 60 |
60 llvm::Optional<FoldingRange> | 61 std::optional<FoldingRange> |
61 extractFoldingRange(const syntax::Node *Node, | 62 extractFoldingRange(const syntax::Node *Node, |
62 const syntax::TokenBufferTokenManager &TM) { | 63 const syntax::TokenBufferTokenManager &TM) { |
63 if (const auto *Stmt = dyn_cast<syntax::CompoundStatement>(Node)) { | 64 if (const auto *Stmt = dyn_cast<syntax::CompoundStatement>(Node)) { |
64 const auto *LBrace = cast_or_null<syntax::Leaf>( | 65 const auto *LBrace = cast_or_null<syntax::Leaf>( |
65 Stmt->findChild(syntax::NodeRole::OpenParen)); | 66 Stmt->findChild(syntax::NodeRole::OpenParen)); |
67 // statements have only one pair of braces so this is valid but for other | 68 // statements have only one pair of braces so this is valid but for other |
68 // node kinds it might not be correct. | 69 // node kinds it might not be correct. |
69 const auto *RBrace = cast_or_null<syntax::Leaf>( | 70 const auto *RBrace = cast_or_null<syntax::Leaf>( |
70 Stmt->findChild(syntax::NodeRole::CloseParen)); | 71 Stmt->findChild(syntax::NodeRole::CloseParen)); |
71 if (!LBrace || !RBrace) | 72 if (!LBrace || !RBrace) |
72 return llvm::None; | 73 return std::nullopt; |
73 // Fold the entire range within braces, including whitespace. | 74 // Fold the entire range within braces, including whitespace. |
74 const SourceLocation LBraceLocInfo = | 75 const SourceLocation LBraceLocInfo = |
75 TM.getToken(LBrace->getTokenKey())->endLocation(), | 76 TM.getToken(LBrace->getTokenKey())->endLocation(), |
76 RBraceLocInfo = | 77 RBraceLocInfo = |
77 TM.getToken(RBrace->getTokenKey())->location(); | 78 TM.getToken(RBrace->getTokenKey())->location(); |
80 // Do not generate folding range for compound statements without any | 81 // Do not generate folding range for compound statements without any |
81 // nodes and newlines. | 82 // nodes and newlines. |
82 if (Range && Range->startLine != Range->endLine) | 83 if (Range && Range->startLine != Range->endLine) |
83 return Range; | 84 return Range; |
84 } | 85 } |
85 return llvm::None; | 86 return std::nullopt; |
86 } | 87 } |
87 | 88 |
88 // Traverse the tree and collect folding ranges along the way. | 89 // Traverse the tree and collect folding ranges along the way. |
89 std::vector<FoldingRange> | 90 std::vector<FoldingRange> |
90 collectFoldingRanges(const syntax::Node *Root, | 91 collectFoldingRanges(const syntax::Node *Root, |
151 // Convert to the LSP linked-list representation. | 152 // Convert to the LSP linked-list representation. |
152 SelectionRange Head; | 153 SelectionRange Head; |
153 Head.range = std::move(Ranges.front()); | 154 Head.range = std::move(Ranges.front()); |
154 SelectionRange *Tail = &Head; | 155 SelectionRange *Tail = &Head; |
155 for (auto &Range : | 156 for (auto &Range : |
156 llvm::makeMutableArrayRef(Ranges.data(), Ranges.size()).drop_front()) { | 157 llvm::MutableArrayRef(Ranges.data(), Ranges.size()).drop_front()) { |
157 Tail->parent = std::make_unique<SelectionRange>(); | 158 Tail->parent = std::make_unique<SelectionRange>(); |
158 Tail = Tail->parent.get(); | 159 Tail = Tail->parent.get(); |
159 Tail->range = std::move(Range); | 160 Tail->range = std::move(Range); |
160 } | 161 } |
161 | 162 |