Truly independent web browser
/*
* Copyright (c) 2024, Matthew Olsson <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Frontend/FrontendAction.h>
struct LibJSCellMacro {
enum class Type {
GCCell,
JSObject,
JSEnvironment,
JSPrototypeObject,
WebWrappable,
};
struct Arg {
std::string text;
clang::SourceLocation location;
};
clang::SourceRange range;
Type type;
std::vector<Arg> args;
static char const* type_name(Type);
};
using LibJSCellMacroMap = std::unordered_map<unsigned int, std::vector<LibJSCellMacro>>;
using PolymorphicSubclasses = std::unordered_map<clang::CXXRecordDecl const*, std::vector<clang::CXXRecordDecl const*>>;
class LibJSPPCallbacks : public clang::PPCallbacks {
public:
LibJSPPCallbacks(clang::Preprocessor& preprocessor, LibJSCellMacroMap& macro_map)
: m_preprocessor(preprocessor)
, m_macro_map(macro_map)
{
}
virtual void LexedFileChanged(clang::FileID curr_fid, LexedFileChangeReason, clang::SrcMgr::CharacteristicKind, clang::FileID, clang::SourceLocation) override;
virtual void MacroExpands(clang::Token const& name_token, clang::MacroDefinition const& definition, clang::SourceRange range, clang::MacroArgs const* args) override;
private:
clang::Preprocessor& m_preprocessor;
std::vector<unsigned int> m_curr_fid_hash_stack;
LibJSCellMacroMap& m_macro_map;
};
class LibJSGCVisitor : public clang::RecursiveASTVisitor<LibJSGCVisitor> {
public:
explicit LibJSGCVisitor(clang::ASTContext& context, LibJSCellMacroMap const& macro_map, bool detect_invalid_function_members)
: m_context(context)
, m_macro_map(macro_map)
, m_detect_invalid_function_members(detect_invalid_function_members)
{
}
bool VisitCXXRecordDecl(clang::CXXRecordDecl*);
bool VisitCXXMethodDecl(clang::CXXMethodDecl*);
bool VisitCXXConstructExpr(clang::CXXConstructExpr*);
bool VisitClassTemplateDecl(clang::ClassTemplateDecl*);
void validate_claims_of_no_cell_edges();
bool VisitVarDecl(clang::VarDecl*);
bool VisitCXXForRangeStmt(clang::CXXForRangeStmt*);
bool VisitFunctionDecl(clang::FunctionDecl*);
bool VisitLambdaExpr(clang::LambdaExpr*);
private:
struct CellMacroExpectation {
LibJSCellMacro::Type type;
std::string base_name;
};
void validate_record_macros(clang::CXXRecordDecl const&);
void validate_engine_subclass_is_in_libjs(clang::CXXRecordDecl const&);
void validate_gc_members_of_polymorphic_record(clang::CXXRecordDecl const&);
void remember_claim_of_no_cell_edges(clang::CXXRecordDecl const&);
void validate_claim_of_no_cell_edges(clang::CXXRecordDecl const&, bool callbacks_hold_roots, clang::CXXRecordDecl const* claiming_base = nullptr);
void remember_polymorphic_subclass(clang::CXXRecordDecl const&);
void validate_object_on_stack(clang::VarDecl const&, clang::SourceLocation);
void validate_type_on_stack(clang::QualType, clang::SourceLocation);
CellMacroExpectation get_record_cell_macro_expectation(clang::CXXRecordDecl const&);
clang::ASTContext& m_context;
LibJSCellMacroMap const& m_macro_map;
bool m_detect_invalid_function_members;
PolymorphicSubclasses m_polymorphic_subclasses;
struct ClaimOfNoCellEdges {
clang::CXXRecordDecl const* record;
bool callbacks_hold_roots;
bool covers_subclasses;
};
std::vector<ClaimOfNoCellEdges> m_claims_of_no_cell_edges;
};
class LibJSGCASTConsumer : public clang::ASTConsumer {
public:
LibJSGCASTConsumer(clang::CompilerInstance&, bool detect_invalid_function_members);
private:
virtual void HandleTranslationUnit(clang::ASTContext& context) override;
clang::CompilerInstance& m_compiler;
LibJSCellMacroMap m_macro_map;
bool m_detect_invalid_function_members;
};
class LibJSGCPluginAction : public clang::PluginASTAction {
public:
virtual bool ParseArgs(clang::CompilerInstance const&, std::vector<std::string> const& args) override
{
m_detect_invalid_function_members = std::find(args.begin(), args.end(), "detect-invalid-function-members") != args.end();
return true;
}
virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance& compiler, llvm::StringRef) override
{
return std::make_unique<LibJSGCASTConsumer>(compiler, m_detect_invalid_function_members);
}
ActionType getActionType() override
{
return AddAfterMainAction;
}
private:
bool m_detect_invalid_function_members { false };
};