2021-10-30 04:25:12 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/ModuleResolver.h"
|
|
|
|
#include "Luau/TypeInfer.h"
|
|
|
|
#include "Luau/BuiltinDefinitions.h"
|
|
|
|
#include "Luau/Frontend.h"
|
|
|
|
#include "Luau/TypeAttach.h"
|
|
|
|
#include "Luau/Transpiler.h"
|
|
|
|
|
|
|
|
#include "FileUtils.h"
|
2022-07-22 04:36:41 +08:00
|
|
|
#include "Flags.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-05-06 03:57:12 +08:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
|
|
|
#include <queue>
|
|
|
|
#include <thread>
|
|
|
|
#include <utility>
|
|
|
|
|
2022-07-08 09:05:31 +08:00
|
|
|
#ifdef CALLGRIND
|
|
|
|
#include <valgrind/callgrind.h>
|
|
|
|
#endif
|
|
|
|
|
2022-01-15 00:06:31 +08:00
|
|
|
LUAU_FASTFLAG(DebugLuauTimeTracing)
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
enum class ReportFormat
|
|
|
|
{
|
|
|
|
Default,
|
2021-12-11 05:17:10 +08:00
|
|
|
Luacheck,
|
|
|
|
Gnu,
|
2021-10-30 04:25:12 +08:00
|
|
|
};
|
|
|
|
|
2021-12-11 05:17:10 +08:00
|
|
|
static void report(ReportFormat format, const char* name, const Luau::Location& loc, const char* type, const char* message)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case ReportFormat::Default:
|
2021-12-11 05:17:10 +08:00
|
|
|
fprintf(stderr, "%s(%d,%d): %s: %s\n", name, loc.begin.line + 1, loc.begin.column + 1, type, message);
|
2021-10-30 04:25:12 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ReportFormat::Luacheck:
|
|
|
|
{
|
|
|
|
// Note: luacheck's end column is inclusive but our end column is exclusive
|
|
|
|
// In addition, luacheck doesn't support multi-line messages, so if the error is multiline we'll fake end column as 100 and hope for the best
|
2021-12-11 05:17:10 +08:00
|
|
|
int columnEnd = (loc.begin.line == loc.end.line) ? loc.end.column : 100;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2021-12-11 05:17:10 +08:00
|
|
|
// Use stdout to match luacheck behavior
|
|
|
|
fprintf(stdout, "%s:%d:%d-%d: (W0) %s: %s\n", name, loc.begin.line + 1, loc.begin.column + 1, columnEnd, type, message);
|
2021-10-30 04:25:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-12-11 05:17:10 +08:00
|
|
|
|
|
|
|
case ReportFormat::Gnu:
|
|
|
|
// Note: GNU end column is inclusive but our end column is exclusive
|
|
|
|
fprintf(stderr, "%s:%d.%d-%d.%d: %s: %s\n", name, loc.begin.line + 1, loc.begin.column + 1, loc.end.line + 1, loc.end.column, type, message);
|
|
|
|
break;
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 05:29:34 +08:00
|
|
|
static void reportError(const Luau::Frontend& frontend, ReportFormat format, const Luau::TypeError& error)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-01-28 05:29:34 +08:00
|
|
|
std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(error.moduleName);
|
2021-11-12 10:12:39 +08:00
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
if (const Luau::SyntaxError* syntaxError = Luau::get_if<Luau::SyntaxError>(&error.data))
|
2022-01-28 05:29:34 +08:00
|
|
|
report(format, humanReadableName.c_str(), error.location, "SyntaxError", syntaxError->message.c_str());
|
2022-10-22 01:33:43 +08:00
|
|
|
else
|
2022-06-24 09:44:07 +08:00
|
|
|
report(format, humanReadableName.c_str(), error.location, "TypeError",
|
|
|
|
Luau::toString(error, Luau::TypeErrorToStringOptions{frontend.fileResolver}).c_str());
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void reportWarning(ReportFormat format, const char* name, const Luau::LintWarning& warning)
|
|
|
|
{
|
|
|
|
report(format, name, warning.location, Luau::LintWarning::getName(warning.code), warning.text.c_str());
|
|
|
|
}
|
|
|
|
|
2023-05-06 03:57:12 +08:00
|
|
|
static bool reportModuleResult(Luau::Frontend& frontend, const Luau::ModuleName& name, ReportFormat format, bool annotate)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-05-06 03:57:12 +08:00
|
|
|
std::optional<Luau::CheckResult> cr = frontend.getCheckResult(name, false);
|
2021-11-12 10:12:39 +08:00
|
|
|
|
2023-05-06 03:57:12 +08:00
|
|
|
if (!cr)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to find result for %s\n", name.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
if (!frontend.getSourceModule(name))
|
|
|
|
{
|
2023-05-06 03:57:12 +08:00
|
|
|
fprintf(stderr, "Error opening %s\n", name.c_str());
|
2021-10-30 04:25:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-06 03:57:12 +08:00
|
|
|
for (auto& error : cr->errors)
|
2022-01-28 05:29:34 +08:00
|
|
|
reportError(frontend, format, error);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-01-28 05:29:34 +08:00
|
|
|
std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(name);
|
2023-05-06 03:57:12 +08:00
|
|
|
for (auto& error : cr->lintResult.errors)
|
2022-01-28 05:29:34 +08:00
|
|
|
reportWarning(format, humanReadableName.c_str(), error);
|
2023-05-06 03:57:12 +08:00
|
|
|
for (auto& warning : cr->lintResult.warnings)
|
2022-01-28 05:29:34 +08:00
|
|
|
reportWarning(format, humanReadableName.c_str(), warning);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
if (annotate)
|
|
|
|
{
|
|
|
|
Luau::SourceModule* sm = frontend.getSourceModule(name);
|
|
|
|
Luau::ModulePtr m = frontend.moduleResolver.getModule(name);
|
|
|
|
|
|
|
|
Luau::attachTypeData(*sm, *m);
|
|
|
|
|
|
|
|
std::string annotated = Luau::transpileWithTypes(*sm->root);
|
|
|
|
|
|
|
|
printf("%s", annotated.c_str());
|
|
|
|
}
|
|
|
|
|
2023-05-06 03:57:12 +08:00
|
|
|
return cr->errors.empty() && cr->lintResult.errors.empty();
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void displayHelp(const char* argv0)
|
|
|
|
{
|
|
|
|
printf("Usage: %s [--mode] [options] [file list]\n", argv0);
|
|
|
|
printf("\n");
|
|
|
|
printf("Available modes:\n");
|
|
|
|
printf(" omitted: typecheck and lint input files\n");
|
|
|
|
printf(" --annotate: typecheck input files and output source with type annotations\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("Available options:\n");
|
|
|
|
printf(" --formatter=plain: report analysis errors in Luacheck-compatible format\n");
|
2021-12-11 05:17:10 +08:00
|
|
|
printf(" --formatter=gnu: report analysis errors in GNU-compatible format\n");
|
2022-07-08 09:05:31 +08:00
|
|
|
printf(" --mode=strict: default to strict mode when typechecking\n");
|
2022-01-15 00:06:31 +08:00
|
|
|
printf(" --timetrace: record compiler time tracing information into trace.json\n");
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-01-07 06:10:07 +08:00
|
|
|
static int assertionHandler(const char* expr, const char* file, int line, const char* function)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
printf("%s(%d): ASSERTION FAILED: %s\n", file, line, expr);
|
2023-03-11 03:20:04 +08:00
|
|
|
fflush(stdout);
|
2021-10-30 04:25:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CliFileResolver : Luau::FileResolver
|
|
|
|
{
|
|
|
|
std::optional<Luau::SourceCode> readSource(const Luau::ModuleName& name) override
|
|
|
|
{
|
2022-01-28 05:29:34 +08:00
|
|
|
Luau::SourceCode::Type sourceType;
|
|
|
|
std::optional<std::string> source = std::nullopt;
|
|
|
|
|
|
|
|
// If the module name is "-", then read source from stdin
|
|
|
|
if (name == "-")
|
|
|
|
{
|
|
|
|
source = readStdin();
|
|
|
|
sourceType = Luau::SourceCode::Script;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
source = readFile(name);
|
|
|
|
sourceType = Luau::SourceCode::Module;
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
if (!source)
|
|
|
|
return std::nullopt;
|
|
|
|
|
2022-01-28 05:29:34 +08:00
|
|
|
return Luau::SourceCode{*source, sourceType};
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 10:07:18 +08:00
|
|
|
std::optional<Luau::ModuleInfo> resolveModule(const Luau::ModuleInfo* context, Luau::AstExpr* node) override
|
|
|
|
{
|
|
|
|
if (Luau::AstExprConstantString* expr = node->as<Luau::AstExprConstantString>())
|
|
|
|
{
|
2021-11-12 10:12:39 +08:00
|
|
|
Luau::ModuleName name = std::string(expr->value.data, expr->value.size) + ".luau";
|
2021-12-03 07:20:08 +08:00
|
|
|
if (!readFile(name))
|
2021-11-12 10:12:39 +08:00
|
|
|
{
|
|
|
|
// fall back to .lua if a module with .luau doesn't exist
|
|
|
|
name = std::string(expr->value.data, expr->value.size) + ".lua";
|
|
|
|
}
|
2021-11-05 10:07:18 +08:00
|
|
|
|
|
|
|
return {{name}};
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-01-28 05:29:34 +08:00
|
|
|
|
|
|
|
std::string getHumanReadableModuleName(const Luau::ModuleName& name) const override
|
|
|
|
{
|
|
|
|
if (name == "-")
|
|
|
|
return "stdin";
|
|
|
|
return name;
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CliConfigResolver : Luau::ConfigResolver
|
|
|
|
{
|
|
|
|
Luau::Config defaultConfig;
|
|
|
|
|
|
|
|
mutable std::unordered_map<std::string, Luau::Config> configCache;
|
|
|
|
mutable std::vector<std::pair<std::string, std::string>> configErrors;
|
|
|
|
|
2022-07-08 09:05:31 +08:00
|
|
|
CliConfigResolver(Luau::Mode mode)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-07-08 09:05:31 +08:00
|
|
|
defaultConfig.mode = mode;
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Luau::Config& getConfig(const Luau::ModuleName& name) const override
|
|
|
|
{
|
|
|
|
std::optional<std::string> path = getParentPath(name);
|
|
|
|
if (!path)
|
|
|
|
return defaultConfig;
|
|
|
|
|
|
|
|
return readConfigRec(*path);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Luau::Config& readConfigRec(const std::string& path) const
|
|
|
|
{
|
|
|
|
auto it = configCache.find(path);
|
|
|
|
if (it != configCache.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
std::optional<std::string> parent = getParentPath(path);
|
|
|
|
Luau::Config result = parent ? readConfigRec(*parent) : defaultConfig;
|
|
|
|
|
|
|
|
std::string configPath = joinPaths(path, Luau::kConfigName);
|
|
|
|
|
|
|
|
if (std::optional<std::string> contents = readFile(configPath))
|
|
|
|
{
|
|
|
|
std::optional<std::string> error = Luau::parseConfig(*contents, result);
|
|
|
|
if (error)
|
|
|
|
configErrors.push_back({configPath, *error});
|
|
|
|
}
|
|
|
|
|
|
|
|
return configCache[path] = result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-06 03:57:12 +08:00
|
|
|
struct TaskScheduler
|
|
|
|
{
|
|
|
|
TaskScheduler(unsigned threadCount)
|
|
|
|
: threadCount(threadCount)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < threadCount; i++)
|
|
|
|
{
|
|
|
|
workers.emplace_back([this] {
|
|
|
|
workerFunction();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~TaskScheduler()
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < threadCount; i++)
|
|
|
|
push({});
|
|
|
|
|
|
|
|
for (std::thread& worker : workers)
|
|
|
|
worker.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void()> pop()
|
|
|
|
{
|
|
|
|
std::unique_lock guard(mtx);
|
|
|
|
|
|
|
|
cv.wait(guard, [this] {
|
|
|
|
return !tasks.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
std::function<void()> task = tasks.front();
|
|
|
|
tasks.pop();
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
void push(std::function<void()> task)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::unique_lock guard(mtx);
|
|
|
|
tasks.push(std::move(task));
|
|
|
|
}
|
|
|
|
|
|
|
|
cv.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getThreadCount()
|
|
|
|
{
|
|
|
|
return std::max(std::thread::hardware_concurrency(), 1u);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void workerFunction()
|
|
|
|
{
|
|
|
|
while (std::function<void()> task = pop())
|
|
|
|
task();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned threadCount = 1;
|
|
|
|
std::mutex mtx;
|
|
|
|
std::condition_variable cv;
|
|
|
|
std::vector<std::thread> workers;
|
|
|
|
std::queue<std::function<void()>> tasks;
|
|
|
|
};
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
Luau::assertHandler() = assertionHandler;
|
|
|
|
|
2022-07-22 04:36:41 +08:00
|
|
|
setLuauFlagsDefault();
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
if (argc >= 2 && strcmp(argv[1], "--help") == 0)
|
|
|
|
{
|
|
|
|
displayHelp(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportFormat format = ReportFormat::Default;
|
2022-07-08 09:05:31 +08:00
|
|
|
Luau::Mode mode = Luau::Mode::Nonstrict;
|
2021-10-30 04:25:12 +08:00
|
|
|
bool annotate = false;
|
2023-05-06 03:57:12 +08:00
|
|
|
int threadCount = 0;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
{
|
|
|
|
if (argv[i][0] != '-')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(argv[i], "--formatter=plain") == 0)
|
|
|
|
format = ReportFormat::Luacheck;
|
2021-12-11 05:17:10 +08:00
|
|
|
else if (strcmp(argv[i], "--formatter=gnu") == 0)
|
|
|
|
format = ReportFormat::Gnu;
|
2022-07-08 09:05:31 +08:00
|
|
|
else if (strcmp(argv[i], "--mode=strict") == 0)
|
|
|
|
mode = Luau::Mode::Strict;
|
2021-10-30 04:25:12 +08:00
|
|
|
else if (strcmp(argv[i], "--annotate") == 0)
|
|
|
|
annotate = true;
|
2022-01-15 00:06:31 +08:00
|
|
|
else if (strcmp(argv[i], "--timetrace") == 0)
|
|
|
|
FFlag::DebugLuauTimeTracing.value = true;
|
2022-07-22 04:36:41 +08:00
|
|
|
else if (strncmp(argv[i], "--fflags=", 9) == 0)
|
|
|
|
setLuauFlags(argv[i] + 9);
|
2023-05-06 03:57:12 +08:00
|
|
|
else if (strncmp(argv[i], "-j", 2) == 0)
|
2023-12-16 04:52:08 +08:00
|
|
|
threadCount = int(strtol(argv[i] + 2, nullptr, 10));
|
2022-01-15 00:06:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(LUAU_ENABLE_TIME_TRACE)
|
|
|
|
if (FFlag::DebugLuauTimeTracing)
|
|
|
|
{
|
2022-07-22 04:36:41 +08:00
|
|
|
fprintf(stderr, "To run with --timetrace, Luau has to be built with LUAU_ENABLE_TIME_TRACE enabled\n");
|
2022-01-15 00:06:31 +08:00
|
|
|
return 1;
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
2022-01-15 00:06:31 +08:00
|
|
|
#endif
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
Luau::FrontendOptions frontendOptions;
|
|
|
|
frontendOptions.retainFullTypeGraphs = annotate;
|
2023-04-08 03:56:27 +08:00
|
|
|
frontendOptions.runLintChecks = true;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
CliFileResolver fileResolver;
|
2022-07-08 09:05:31 +08:00
|
|
|
CliConfigResolver configResolver(mode);
|
2021-10-30 04:25:12 +08:00
|
|
|
Luau::Frontend frontend(&fileResolver, &configResolver, frontendOptions);
|
|
|
|
|
2023-04-08 03:56:27 +08:00
|
|
|
Luau::registerBuiltinGlobals(frontend, frontend.globals);
|
2023-03-11 03:20:04 +08:00
|
|
|
Luau::freeze(frontend.globals.globalTypes);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-07-08 09:05:31 +08:00
|
|
|
#ifdef CALLGRIND
|
|
|
|
CALLGRIND_ZERO_STATS;
|
|
|
|
#endif
|
|
|
|
|
2021-11-19 06:21:07 +08:00
|
|
|
std::vector<std::string> files = getSourceFiles(argc, argv);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-05-06 03:57:12 +08:00
|
|
|
for (const std::string& path : files)
|
|
|
|
frontend.queueModuleCheck(path);
|
|
|
|
|
|
|
|
std::vector<Luau::ModuleName> checkedModules;
|
|
|
|
|
|
|
|
// If thread count is not set, try to use HW thread count, but with an upper limit
|
|
|
|
// When we improve scalability of typechecking, upper limit can be adjusted/removed
|
|
|
|
if (threadCount <= 0)
|
|
|
|
threadCount = std::min(TaskScheduler::getThreadCount(), 8u);
|
|
|
|
|
2023-12-16 04:52:08 +08:00
|
|
|
try
|
2023-05-06 03:57:12 +08:00
|
|
|
{
|
|
|
|
TaskScheduler scheduler(threadCount);
|
|
|
|
|
|
|
|
checkedModules = frontend.checkQueuedModules(std::nullopt, [&](std::function<void()> f) {
|
|
|
|
scheduler.push(std::move(f));
|
|
|
|
});
|
|
|
|
}
|
2023-12-16 04:52:08 +08:00
|
|
|
catch (const Luau::InternalCompilerError& ice)
|
|
|
|
{
|
|
|
|
Luau::Location location = ice.location ? *ice.location : Luau::Location();
|
|
|
|
|
|
|
|
std::string moduleName = ice.moduleName ? *ice.moduleName : "<unknown module>";
|
|
|
|
std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(moduleName);
|
|
|
|
|
|
|
|
Luau::TypeError error(location, moduleName, Luau::InternalError{ice.message});
|
|
|
|
|
|
|
|
report(format, humanReadableName.c_str(), location, "InternalCompilerError",
|
|
|
|
Luau::toString(error, Luau::TypeErrorToStringOptions{frontend.fileResolver}).c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
2023-05-06 03:57:12 +08:00
|
|
|
|
2021-11-19 06:21:07 +08:00
|
|
|
int failed = 0;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-05-06 03:57:12 +08:00
|
|
|
for (const Luau::ModuleName& name : checkedModules)
|
|
|
|
failed += !reportModuleResult(frontend, name, format, annotate);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
if (!configResolver.configErrors.empty())
|
|
|
|
{
|
|
|
|
failed += int(configResolver.configErrors.size());
|
|
|
|
|
|
|
|
for (const auto& pair : configResolver.configErrors)
|
|
|
|
fprintf(stderr, "%s: %s\n", pair.first.c_str(), pair.second.c_str());
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:06:31 +08:00
|
|
|
if (format == ReportFormat::Luacheck)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return failed ? 1 : 0;
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|