Skip to content

Commit 62c9940

Browse files
authored
Merge branch 'main' into fix/163224
2 parents adefaa1 + d148407 commit 62c9940

File tree

224 files changed

+14006
-7239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

224 files changed

+14006
-7239
lines changed

clang-tools-extra/clang-tidy/objc/AssertEqualsCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void AssertEqualsCheck::registerMatchers(MatchFinder *Finder) {
2525
for (const auto &[CurrName, _] : NameMap) {
2626
Finder->addMatcher(
2727
binaryOperator(anyOf(hasOperatorName("!="), hasOperatorName("==")),
28-
isExpandedFromMacro(CurrName),
28+
isExpandedFromMacro(std::string(CurrName)),
2929
anyOf(hasLHS(hasType(qualType(
3030
hasCanonicalType(asString("NSString *"))))),
3131
hasRHS(hasType(qualType(

clang-tools-extra/clangd/SemanticSelection.cpp

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
#include "Protocol.h"
1212
#include "Selection.h"
1313
#include "SourceCode.h"
14+
#include "support/Bracket.h"
15+
#include "support/DirectiveTree.h"
16+
#include "support/Token.h"
1417
#include "clang/AST/DeclBase.h"
1518
#include "clang/Basic/SourceLocation.h"
1619
#include "clang/Basic/SourceManager.h"
20+
#include "clang/Basic/TokenKinds.h"
1721
#include "clang/Tooling/Syntax/BuildTree.h"
1822
#include "clang/Tooling/Syntax/Nodes.h"
1923
#include "clang/Tooling/Syntax/TokenBufferTokenManager.h"
@@ -22,9 +26,6 @@
2226
#include "llvm/ADT/StringRef.h"
2327
#include "llvm/Support/Casting.h"
2428
#include "llvm/Support/Error.h"
25-
#include "support/Bracket.h"
26-
#include "support/DirectiveTree.h"
27-
#include "support/Token.h"
2829
#include <optional>
2930
#include <queue>
3031
#include <vector>
@@ -163,6 +164,69 @@ llvm::Expected<SelectionRange> getSemanticRanges(ParsedAST &AST, Position Pos) {
163164
return std::move(Head);
164165
}
165166

167+
class PragmaRegionFinder {
168+
// Record the token range of a region:
169+
//
170+
// #pragma region name[[
171+
// ...
172+
// ]]#pragma endregion
173+
std::vector<Token::Range> &Ranges;
174+
const TokenStream &Code;
175+
// Stack of starting token (the name of the region) indices for nested #pragma
176+
// region.
177+
std::vector<unsigned> Stack;
178+
179+
public:
180+
PragmaRegionFinder(std::vector<Token::Range> &Ranges, const TokenStream &Code)
181+
: Ranges(Ranges), Code(Code) {}
182+
183+
void walk(const DirectiveTree &T) {
184+
for (const auto &C : T.Chunks)
185+
std::visit(*this, C);
186+
}
187+
188+
void operator()(const DirectiveTree::Code &C) {}
189+
190+
void operator()(const DirectiveTree::Directive &D) {
191+
// Get the tokens that make up this directive.
192+
auto Tokens = Code.tokens(D.Tokens);
193+
if (Tokens.empty())
194+
return;
195+
const Token &HashToken = Tokens.front();
196+
assert(HashToken.Kind == tok::hash);
197+
const Token &Pragma = HashToken.nextNC();
198+
if (Pragma.text() != "pragma")
199+
return;
200+
const Token &Value = Pragma.nextNC();
201+
202+
// Handle "#pragma region name"
203+
if (Value.text() == "region") {
204+
// Find the last token at the same line.
205+
const Token *T = &Value.next();
206+
while (T < Tokens.end() && T->Line == Pragma.Line)
207+
T = &T->next();
208+
--T;
209+
Stack.push_back(T->OriginalIndex);
210+
return;
211+
}
212+
213+
// Handle "#pragma endregion"
214+
if (Value.text() == "endregion") {
215+
if (Stack.empty())
216+
return; // unmatched end region; ignore.
217+
218+
unsigned StartIdx = Stack.back();
219+
Stack.pop_back();
220+
Ranges.push_back(Token::Range{StartIdx, HashToken.OriginalIndex});
221+
}
222+
}
223+
224+
void operator()(const DirectiveTree::Conditional &C) {
225+
for (const auto &[_, SubTree] : C.Branches)
226+
walk(SubTree);
227+
}
228+
};
229+
166230
// FIXME(kirillbobyrev): Collect comments, PP conditional regions, includes and
167231
// other code regions (e.g. public/private/protected sections of classes,
168232
// control flow statement bodies).
@@ -286,6 +350,17 @@ getFoldingRanges(const std::string &Code, bool LineFoldingOnly) {
286350
}
287351
AddFoldingRange(Start, End, FoldingRange::COMMENT_KIND);
288352
}
353+
354+
// #pragma region
355+
std::vector<Token::Range> Ranges;
356+
PragmaRegionFinder(Ranges, OrigStream).walk(DirectiveStructure);
357+
auto Ts = OrigStream.tokens();
358+
for (const auto &R : Ranges) {
359+
auto End = StartPosition(Ts[R.End]);
360+
if (LineFoldingOnly)
361+
End.line--;
362+
AddFoldingRange(EndPosition(Ts[R.Begin]), End, FoldingRange::REGION_KIND);
363+
}
289364
return Result;
290365
}
291366

clang-tools-extra/clangd/unittests/SemanticSelectionTests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,22 @@ TEST(FoldingRanges, PseudoParserWithoutLineFoldings) {
410410
Variable = 3;
411411
#
412412
)cpp",
413+
R"cpp(
414+
#pragma region R1[[
415+
416+
#pragma region R2[[
417+
constexpr int a = 2;
418+
]]#pragma endregion
419+
420+
]]#pragma endregion
421+
)cpp",
422+
R"cpp(
423+
#pragma region[[
424+
]]#pragma endregion
425+
426+
#pragma /*comment1*/ region /*comment2*/name[[
427+
]]#pragma endregion
428+
)cpp",
413429
};
414430
for (const char *Test : Tests) {
415431
auto T = Annotations(Test);
@@ -470,6 +486,12 @@ TEST(FoldingRanges, PseudoParserLineFoldingsOnly) {
470486
//[[ foo
471487
/* bar */]]
472488
)cpp",
489+
R"cpp(
490+
#pragma region abc[[
491+
constexpr int a = 2;
492+
]]
493+
#pragma endregion
494+
)cpp",
473495
// FIXME: Support folding template arguments.
474496
// R"cpp(
475497
// template <[[typename foo, class bar]]> struct baz {};

clang/docs/LibASTMatchersReference.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,7 +4222,7 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
42224222
</pre></td></tr>
42234223

42244224

4225-
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('isExpandedFromMacro0')"><a name="isExpandedFromMacro0Anchor">isExpandedFromMacro</a></td><td>StringRef MacroName</td></tr>
4225+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('isExpandedFromMacro0')"><a name="isExpandedFromMacro0Anchor">isExpandedFromMacro</a></td><td>std::string MacroName</td></tr>
42264226
<tr><td colspan="4" class="doc" id="isExpandedFromMacro0"><pre>Matches statements that are (transitively) expanded from the named macro.
42274227
Does not match if only part of the statement is expanded from that macro or
42284228
if different parts of the statement are expanded from different
@@ -5643,7 +5643,7 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
56435643
</pre></td></tr>
56445644

56455645

5646-
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>&gt;</td><td class="name" onclick="toggle('isExpandedFromMacro1')"><a name="isExpandedFromMacro1Anchor">isExpandedFromMacro</a></td><td>StringRef MacroName</td></tr>
5646+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>&gt;</td><td class="name" onclick="toggle('isExpandedFromMacro1')"><a name="isExpandedFromMacro1Anchor">isExpandedFromMacro</a></td><td>std::string MacroName</td></tr>
56475647
<tr><td colspan="4" class="doc" id="isExpandedFromMacro1"><pre>Matches statements that are (transitively) expanded from the named macro.
56485648
Does not match if only part of the statement is expanded from that macro or
56495649
if different parts of the statement are expanded from different
@@ -5843,7 +5843,7 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
58435843
</pre></td></tr>
58445844

58455845

5846-
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1TypeLoc.html">TypeLoc</a>&gt;</td><td class="name" onclick="toggle('isExpandedFromMacro2')"><a name="isExpandedFromMacro2Anchor">isExpandedFromMacro</a></td><td>StringRef MacroName</td></tr>
5846+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1TypeLoc.html">TypeLoc</a>&gt;</td><td class="name" onclick="toggle('isExpandedFromMacro2')"><a name="isExpandedFromMacro2Anchor">isExpandedFromMacro</a></td><td>std::string MacroName</td></tr>
58475847
<tr><td colspan="4" class="doc" id="isExpandedFromMacro2"><pre>Matches statements that are (transitively) expanded from the named macro.
58485848
Does not match if only part of the statement is expanded from that macro or
58495849
if different parts of the statement are expanded from different

clang/include/clang/AST/ASTImporter.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ class TypeSourceInfo;
190190
llvm::SmallDenseMap<Decl *, int, 32> Aux;
191191
};
192192

193+
class FunctionDeclImportCycleDetector {
194+
public:
195+
auto makeScopedCycleDetection(const FunctionDecl *D);
196+
197+
bool isCycle(const FunctionDecl *D) const;
198+
199+
private:
200+
llvm::DenseSet<const FunctionDecl *> FunctionDeclsWithImportInProgress;
201+
};
202+
193203
private:
194204
std::shared_ptr<ASTImporterSharedState> SharedState = nullptr;
195205

@@ -254,6 +264,12 @@ class TypeSourceInfo;
254264
/// Declaration (from, to) pairs that are known not to be equivalent
255265
/// (which we have already complained about).
256266
NonEquivalentDeclSet NonEquivalentDecls;
267+
/// A FunctionDecl can have properties that have a reference to the
268+
/// function itself and are imported before the function is created. This
269+
/// can come for example from auto return type or when template parameters
270+
/// are used in the return type or parameters. This member is used to detect
271+
/// cyclic import of FunctionDecl objects to avoid infinite recursion.
272+
FunctionDeclImportCycleDetector FindFunctionDeclImportCycle;
257273

258274
using FoundDeclsTy = SmallVector<NamedDecl *, 2>;
259275
FoundDeclsTy findDeclsInToCtx(DeclContext *DC, DeclarationName Name);

0 commit comments

Comments
 (0)