mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:30:14 +01:00
Most of these are places where we failed to include a header file and simply never got an error about this. The fix is to include the header file. Most other cases are functions that should have been marked `static` but were not. Finding all of these was a main motivation for me enabling the warning despite how much work it is. One complicating factor was that we weren't including the `handle.h` for all the state-based handler functions. While this isn't a tiny amount of code, it is just declarations and doesn't add any extra dependencies. It also lets us have the checking for which functions need to be `static` and which don't. For the `parse` library I had to add the `handle.h` header as well, I tried to match the design of it in `check`. I have also had to work around a bug in the warning, but given the value it seems to be providing, that seems reasonable. I've filed the bug upstream: https://github.com/llvm/llvm-project/issues/94138 I also had to use some hacks to work around limitations of Bazel rules that wrap `cc_library` rules and don't expose `copts`. I filed a bug for `cc_proto_library` specifically: ~https://github.com/bazelbuild/bazel/issues/22610~ https://github.com/bazelbuild/bazel/issues/4446
93 lines
3.1 KiB
C++
93 lines
3.1 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/parse/context.h"
|
|
#include "toolchain/parse/handle.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
auto HandleDeclNameAndParams(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier);
|
|
if (!identifier) {
|
|
Lex::TokenIndex token = *context.position();
|
|
if (context.tokens().GetKind(token) == Lex::TokenKind::FileEnd) {
|
|
// The end of file is an unhelpful diagnostic location. Instead, use the
|
|
// introducer token.
|
|
token = state.token;
|
|
}
|
|
if (state.token == *context.position()) {
|
|
CARBON_DIAGNOSTIC(ExpectedDeclNameAfterPeriod, Error,
|
|
"`.` should be followed by a name.");
|
|
context.emitter().Emit(token, ExpectedDeclNameAfterPeriod);
|
|
} else {
|
|
CARBON_DIAGNOSTIC(ExpectedDeclName, Error,
|
|
"`{0}` introducer should be followed by a name.",
|
|
Lex::TokenKind);
|
|
context.emitter().Emit(token, ExpectedDeclName,
|
|
context.tokens().GetKind(state.token));
|
|
}
|
|
context.ReturnErrorOnState();
|
|
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
|
/*has_error=*/true);
|
|
return;
|
|
}
|
|
|
|
context.AddLeafNode(NodeKind::IdentifierName, *identifier);
|
|
|
|
switch (context.PositionKind()) {
|
|
case Lex::TokenKind::Period:
|
|
context.AddNode(NodeKind::NameQualifier,
|
|
context.ConsumeChecked(Lex::TokenKind::Period),
|
|
state.subtree_start, state.has_error);
|
|
context.PushState(State::DeclNameAndParams);
|
|
break;
|
|
|
|
case Lex::TokenKind::OpenSquareBracket:
|
|
state.state = State::DeclNameAndParamsAfterImplicit;
|
|
context.PushState(state);
|
|
context.PushState(State::PatternListAsImplicit);
|
|
break;
|
|
|
|
case Lex::TokenKind::OpenParen:
|
|
state.state = State::DeclNameAndParamsAfterParams;
|
|
context.PushState(state);
|
|
context.PushState(State::PatternListAsTuple);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
auto HandleDeclNameAndParamsAfterImplicit(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (!context.PositionIs(Lex::TokenKind::OpenParen)) {
|
|
CARBON_DIAGNOSTIC(
|
|
ParamsRequiredAfterImplicit, Error,
|
|
"A `(` for parameters is required after implicit parameters.");
|
|
context.emitter().Emit(*context.position(), ParamsRequiredAfterImplicit);
|
|
context.ReturnErrorOnState();
|
|
return;
|
|
}
|
|
|
|
state.state = State::DeclNameAndParamsAfterParams;
|
|
context.PushState(state);
|
|
context.PushState(State::PatternListAsTuple);
|
|
}
|
|
|
|
auto HandleDeclNameAndParamsAfterParams(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (auto period = context.ConsumeIf(Lex::TokenKind::Period)) {
|
|
context.AddNode(NodeKind::NameQualifier, *period, state.subtree_start,
|
|
state.has_error);
|
|
context.PushState(State::DeclNameAndParams);
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon::Parse
|