Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@


add_executable(cxx
cxx/cxx.cc
cxx/dump_tokens.cc
cxx/frontend.cc
cxx/verify_diagnostics_client.cc
cxx/check_expression_types.cc
)

target_link_libraries(cxx PRIVATE cxx-lsp)
Expand Down
83 changes: 83 additions & 0 deletions src/frontend/cxx/check_expression_types.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2025 Roberto Raggi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "check_expression_types.h"

#include <cxx/ast.h>
#include <cxx/ast_visitor.h>

#include <format>

namespace cxx {

namespace {

class CheckExpressionTypes final : private ASTVisitor {
public:
[[nodiscard]] auto operator()(TranslationUnit* unit) {
std::size_t missingTypes = 0;
std::swap(unit_, unit);
std::swap(missingTypes_, missingTypes);

accept(unit_->ast());

std::swap(unit_, unit);
std::swap(missingTypes_, missingTypes);

return missingTypes == 0;
}

private:
using ASTVisitor::visit;

auto preVisit(AST* ast) -> bool override {
if (ast_cast<TemplateDeclarationAST>(ast)) {
// skip template declarations, as they are not instantiated yet
return false;
}

if (auto expression = ast_cast<ExpressionAST>(ast)) {
if (!expression->type) {
const auto loc = expression->firstSourceLocation();

unit_->warning(loc, std::format("untyped expression of kind '{}'",
to_string(expression->kind())));

++missingTypes_;
return false;
}
}

return true; // visit children
}

private:
TranslationUnit* unit_ = nullptr;
std::size_t missingTypes_ = 0;
};

} // namespace

auto checkExpressionTypes(TranslationUnit& unit) -> bool {
CheckExpressionTypes checkExpressionTypes;
return checkExpressionTypes(&unit);
}

} // namespace cxx
29 changes: 29 additions & 0 deletions src/frontend/cxx/check_expression_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2025 Roberto Raggi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <cxx/translation_unit.h>

namespace cxx {

[[nodiscard]] auto checkExpressionTypes(TranslationUnit& unit) -> bool;

}
65 changes: 65 additions & 0 deletions src/frontend/cxx/cxx.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2025 Roberto Raggi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <cxx/lsp/lsp_server.h>

#include <iostream>

#include "frontend.h"

auto main(int argc, char* argv[]) -> int {
cxx::CLI cli;
cli.parse(argc, argv);

if (cli.opt_help) {
cli.showHelp();
return EXIT_SUCCESS;
}

const auto& inputFiles = cli.positionals();

if (cli.opt_lsp_test) {
cli.opt_lsp = true;
}

if (!cli.opt_lsp && inputFiles.empty()) {
std::cerr << "cxx: no input files" << std::endl
<< "Usage: cxx [options] file..." << std::endl;
return EXIT_FAILURE;
}

if (cli.opt_lsp) {
auto server = cxx::lsp::Server{cli};

return server.start();
}

auto existStatus = EXIT_SUCCESS;

for (const auto& fileName : inputFiles) {
cxx::Frontend runOnFile(cli, fileName);

if (!runOnFile()) {
existStatus = EXIT_FAILURE;
}
}

return existStatus;
}
71 changes: 71 additions & 0 deletions src/frontend/cxx/dump_tokens.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2025 Roberto Raggi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "dump_tokens.h"

#include <cxx/lexer.h>

#include <format>
#include <iostream>

namespace cxx {

DumpTokens::DumpTokens(const CLI& cli) : cli(cli) {}

void DumpTokens::operator()(TranslationUnit& unit, std::ostream& output) {
auto lang = LanguageKind::kCXX;

if (auto x = cli.getSingle("x")) {
if (x == "c") lang = LanguageKind::kC;
} else if (unit.fileName().ends_with(".c")) {
lang = LanguageKind::kC;
}

std::string flags;

for (SourceLocation loc(1);; loc = loc.next()) {
const auto& tk = unit.tokenAt(loc);

flags.clear();

if (tk.startOfLine()) {
flags += " [start-of-line]";
}

if (tk.leadingSpace()) {
flags += " [leading-space]";
}

auto kind = tk.kind();
if (kind == TokenKind::T_IDENTIFIER) {
kind = Lexer::classifyKeyword(tk.spell(), lang);
}

output << std::format("{} '{}'{}", Token::name(kind), tk.spell(), flags);

auto pos = unit.tokenStartPosition(loc);

output << std::format(" at {}:{}:{}\n", pos.fileName, pos.line, pos.column);

if (tk.is(TokenKind::T_EOF_SYMBOL)) break;
}
}

} // namespace cxx
38 changes: 38 additions & 0 deletions src/frontend/cxx/dump_tokens.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2025 Roberto Raggi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <cxx/cli.h>
#include <cxx/translation_unit.h>

namespace cxx {

class DumpTokens {
public:
DumpTokens(const CLI& cli);

void operator()(TranslationUnit& unit, std::ostream& output);

private:
const CLI& cli;
};

} // namespace cxx
Loading
Loading