-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][CodeComplete] Add code completion for if constexpr and consteval #124315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][CodeComplete] Add code completion for if constexpr and consteval #124315
Conversation
@llvm/pr-subscribers-clang Author: FantasqueX (FantasqueX) ChangesC++17 supports Full diff: https://github.com/llvm/llvm-project/pull/124315.diff 3 Files Affected:
diff --git a/clang/include/clang/Sema/SemaCodeCompletion.h b/clang/include/clang/Sema/SemaCodeCompletion.h
index e931596c215d31..af44745d5d1239 100644
--- a/clang/include/clang/Sema/SemaCodeCompletion.h
+++ b/clang/include/clang/Sema/SemaCodeCompletion.h
@@ -152,6 +152,7 @@ class SemaCodeCompletion : public SemaBase {
void CodeCompleteDesignator(const QualType BaseType,
llvm::ArrayRef<Expr *> InitExprs,
const Designation &D);
+ void CodeCompleteIfConstExpr(Scope *S) const;
void CodeCompleteAfterIf(Scope *S, bool IsBracedThen);
void CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, bool EnteringContext,
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index cd4504630f8719..3f9900dd997ada 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -1553,6 +1553,14 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
IsConsteval = true;
ConstevalLoc = ConsumeToken();
}
+
+ if (Tok.is(tok::code_completion)) {
+ if (getLangOpts().CPlusPlus17) {
+ cutOffParsing();
+ Actions.CodeCompletion().CodeCompleteIfConstExpr(getCurScope());
+ return StmtError();
+ }
+ }
}
if (!IsConsteval && (NotLocation.isValid() || Tok.isNot(tok::l_paren))) {
Diag(Tok, diag::err_expected_lparen_after) << "if";
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index 58f3efbe0daf89..b159fd26a45208 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -6762,6 +6762,21 @@ void SemaCodeCompletion::CodeCompleteInitializer(Scope *S, Decl *D) {
CodeCompleteExpression(S, Data);
}
+void SemaCodeCompletion::CodeCompleteIfConstExpr(Scope *S) const {
+ ResultBuilder Results(SemaRef, CodeCompleter->getAllocator(),
+ CodeCompleter->getCodeCompletionTUInfo(),
+ CodeCompletionContext::CCC_SymbolOrNewName);
+ Results.EnterNewScope();
+
+ Results.AddResult(CodeCompletionResult("constexpr"));
+
+ Results.ExitScope();
+
+ HandleCodeCompleteResults(&SemaRef, CodeCompleter,
+ Results.getCompletionContext(), Results.data(),
+ Results.size());
+}
+
void SemaCodeCompletion::CodeCompleteAfterIf(Scope *S, bool IsBracedThen) {
ResultBuilder Results(SemaRef, CodeCompleter->getAllocator(),
CodeCompleter->getCodeCompletionTUInfo(),
|
47f97a5
to
0715933
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! Please add a test for your patch. You can take tests under clang/test/CodeCompletion
for reference.
0715933
to
d276c4a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
c8998ac
to
e6d79ad
Compare
6ae8c5a
to
dcb660f
Compare
If I'm understanding correctly, this patch only completes the keywords themselves. SemaCodeComplete also has the ability to produce "snippets", for example here is a snippet for What do you think about adding similar snippets for |
dcb660f
to
914e63c
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
Hi @HighCommander4 thanks for your suggestions. I'm happy to add snippets support for |
d0d3984
to
e488453
Compare
e488453
to
144facd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I tried out the patch with clangd and it seems to be working pretty well.
A further enhancement that occurs to me is that we could offer these patterns slightly earlier, e.g. when you're at the beginning of an empty line and invoke code completion and then type if
to filter the results, you see the if (condition) {statements}
pattern as the first result -- we could have if constexpr (condition) {statements}
as another result there already. But that can be a future enhancement for another time.
The implementation looks pretty good, just have some minor comments.
bool AfterExclaim) const { | ||
ResultBuilder Results(SemaRef, CodeCompleter->getAllocator(), | ||
CodeCompleter->getCodeCompletionTUInfo(), | ||
CodeCompletionContext::CCC_Other); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the similar function CodeCompleteAfterIf
, I think mapCodeCompletionContext(SemaRef, PCC_Statement)
would be more appropriate here than CodeCompletionContext::CCC_Other
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++17 supports `if constexpr` statement. This patch implements this in code completion.
144facd
to
f49496e
Compare
@HighCommander4 @zyn0217 Really appreciate for your review. Thanks a lot. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM!
C++17 supports
if constexpr
statement. C++23 supportsif consteval
statement. This patch implements this in code completion.