Skip to content

Truly independent web browser

Meta: Add a clang plugin that rejects the C library's math functions

Shannon Boothab987c1ff2ce
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 963894a5e1d..7a8777be45c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -84,6 +84,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
depend_on_clang_plugin(JSClangPlugin LibJSGCClangPlugin)
depend_on_clang_plugin(GenericClangPlugin LambdaCaptureClangPlugin)
depend_on_clang_plugin(GenericClangPlugin KmallocAllocationClangPlugin)
+ depend_on_clang_plugin(GenericClangPlugin MathFunctionsClangPlugin)
endif()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if (ENABLE_FUZZERS_LIBFUZZER)
diff --git a/Meta/ClangPlugins/CMakeLists.txt b/Meta/ClangPlugins/CMakeLists.txt
index 3905d90fcfa..ecc1a181a1d 100644
--- a/Meta/ClangPlugins/CMakeLists.txt
+++ b/Meta/ClangPlugins/CMakeLists.txt
@@ -39,3 +39,4 @@ endfunction()
clang_plugin(LambdaCaptureClangPlugin SOURCES LambdaCapturePluginAction.cpp)
clang_plugin(LibJSGCClangPlugin SOURCES LibJSGCPluginAction.cpp)
clang_plugin(KmallocAllocationClangPlugin SOURCES KmallocAllocationPluginAction.cpp)
+clang_plugin(MathFunctionsClangPlugin SOURCES MathFunctionsPluginAction.cpp)
diff --git a/Meta/ClangPlugins/MathFunctionsPluginAction.cpp b/Meta/ClangPlugins/MathFunctionsPluginAction.cpp
new file mode 100644
index 00000000000..2edc82017e7
--- /dev/null
+++ b/Meta/ClangPlugins/MathFunctionsPluginAction.cpp
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "MathFunctionsPluginAction.h"
+#include <clang/Basic/SourceManager.h>
+#include <clang/Frontend/FrontendPluginRegistry.h>
+#include <llvm/ADT/StringSet.h>
+#include <optional>
+
+// The math functions that the C library of each platform computes differently.
+static llvm::StringSet<> const math_functions = {
+ "acos",
+ "acosh",
+ "asin",
+ "asinh",
+ "atan",
+ "atan2",
+ "atanh",
+ "cbrt",
+ "cos",
+ "cosh",
+ "exp",
+ "exp2",
+ "expm1",
+ "hypot",
+ "log",
+ "log10",
+ "log1p",
+ "log2",
+ "pow",
+ "sin",
+ "sincos",
+ "sinh",
+ "tan",
+ "tanh",
+};
+
+static std::optional<llvm::StringRef> math_function_name(clang::FunctionDecl const& function, clang::SourceManager const& source_manager)
+{
+ if (!function.getDeclName().isIdentifier())
+ return {};
+
+ // A function of our own that shares a name with one of the C library is fine.
+ if (!function.getBuiltinID() && !source_manager.isInSystemHeader(function.getCanonicalDecl()->getLocation()))
+ return {};
+
+ clang::NamespaceDecl const* outermost_namespace = nullptr;
+ for (auto const* context = function.getDeclContext(); context; context = context->getParent()) {
+ if (auto const* namespace_decl = llvm::dyn_cast<clang::NamespaceDecl>(context))
+ outermost_namespace = namespace_decl;
+ }
+ if (outermost_namespace && outermost_namespace->getName() != "std")
+ return {};
+
+ auto name = function.getName();
+ name.consume_front("__builtin_");
+ if (math_functions.contains(name))
+ return name;
+ if ((name.ends_with("f") || name.ends_with("l")) && math_functions.contains(name.drop_back()))
+ return name.drop_back();
+ return {};
+}
+
+bool MathFunctionsVisitor::TraverseDecl(clang::Decl* declaration)
+{
+ if (declaration) {
+ auto location = declaration->getLocation();
+ if (location.isValid() && m_context.getSourceManager().isInSystemHeader(location))
+ return true;
+ }
+ return RecursiveASTVisitor::TraverseDecl(declaration);
+}
+
+bool MathFunctionsVisitor::VisitDeclRefExpr(clang::DeclRefExpr* reference)
+{
+ auto const* function = llvm::dyn_cast<clang::FunctionDecl>(reference->getDecl());
+ if (!function)
+ return true;
+ auto& source_manager = m_context.getSourceManager();
+ auto name = math_function_name(*function, source_manager);
+ if (!name)
+ return true;
+
+ auto location = source_manager.getFileLoc(reference->getLocation());
+ if (source_manager.isInSystemHeader(location))
+ return true;
+
+ auto& diag_engine = m_context.getDiagnostics();
+ auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "%0 is computed differently by the C library of each platform. Use AK::%1 from AK/Math.h instead");
+ diag_engine.Report(reference->getBeginLoc(), diag_id) << function << *name;
+ return true;
+}
+
+void MathFunctionsASTConsumer::HandleTranslationUnit(clang::ASTContext& context)
+{
+ MathFunctionsVisitor visitor(context);
+ visitor.TraverseDecl(context.getTranslationUnitDecl());
+}
+
+static clang::FrontendPluginRegistry::Add<MathFunctionsPluginAction> X("math_functions", "check that math functions come from AK/Math.h");
diff --git a/Meta/ClangPlugins/MathFunctionsPluginAction.h b/Meta/ClangPlugins/MathFunctionsPluginAction.h
new file mode 100644
index 00000000000..117098d7859
--- /dev/null
+++ b/Meta/ClangPlugins/MathFunctionsPluginAction.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <clang/AST/ASTConsumer.h>
+#include <clang/AST/RecursiveASTVisitor.h>
+#include <clang/Frontend/FrontendAction.h>
+
+class MathFunctionsVisitor : public clang::RecursiveASTVisitor<MathFunctionsVisitor> {
+public:
+ explicit MathFunctionsVisitor(clang::ASTContext& context)
+ : m_context(context)
+ {
+ }
+
+ bool shouldVisitTemplateInstantiations() const { return true; }
+
+ bool TraverseDecl(clang::Decl*);
+ bool VisitDeclRefExpr(clang::DeclRefExpr*);
+
+private:
+ clang::ASTContext& m_context;
+};
+
+class MathFunctionsASTConsumer : public clang::ASTConsumer {
+private:
+ virtual void HandleTranslationUnit(clang::ASTContext&) override;
+};
+
+class MathFunctionsPluginAction : public clang::PluginASTAction {
+public:
+ virtual bool ParseArgs(clang::CompilerInstance const&, std::vector<std::string> const&) override
+ {
+ return true;
+ }
+
+ virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance&, llvm::StringRef) override
+ {
+ return std::make_unique<MathFunctionsASTConsumer>();
+ }
+
+ ActionType getActionType() override
+ {
+ return AddAfterMainAction;
+ }
+};
diff --git a/Tests/ClangPlugins/MathFunctionsTests/c_library_math_functions.cpp b/Tests/ClangPlugins/MathFunctionsTests/c_library_math_functions.cpp
new file mode 100644
index 00000000000..2b6959ba4b1
--- /dev/null
+++ b/Tests/ClangPlugins/MathFunctionsTests/c_library_math_functions.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+// RUN: %clang++ -Xclang -verify %plugin_opts% -c %s -o %t 2>&1
+
+#include <AK/Math.h>
+#include <cmath>
+#include <math.h>
+
+double c_library(double x, float y)
+{
+ // expected-error@+1 {{'sin' is computed differently by the C library of each platform. Use AK::sin from AK/Math.h instead}}
+ auto a = sin(x);
+ // expected-error@+1 {{'cbrtf' is computed differently by the C library of each platform. Use AK::cbrt from AK/Math.h instead}}
+ auto b = cbrtf(y);
+ // expected-error@+1 {{'pow' is computed differently by the C library of each platform. Use AK::pow from AK/Math.h instead}}
+ auto c = std::pow(x, x);
+ // expected-error@+1 {{'hypot' is computed differently by the C library of each platform. Use AK::hypot from AK/Math.h instead}}
+ auto d = std::hypot(y, y);
+ // expected-error@+1 {{'__builtin_atan2' is computed differently by the C library of each platform. Use AK::atan2 from AK/Math.h instead}}
+ auto e = __builtin_atan2(x, x);
+ // expected-error@+1 {{'acosh' is computed differently by the C library of each platform. Use AK::acosh from AK/Math.h instead}}
+ double (*f)(double) = acosh;
+ return a + b + c + d + e + f(x);
+}
+
+static double log(char const*)
+{
+ return 0;
+}
+
+double own_function_with_a_c_library_name()
+{
+ return log("message");
+}
+
+double ak(double x, float y)
+{
+ float sin_value;
+ float cos_value;
+ AK::sincos(y, sin_value, cos_value);
+ return AK::sin(x) + AK::pow(y, y) + AK::hypot(x, x) + AK::exp(x) + sin_value + cos_value
+ + AK::sqrt(x) + std::sqrt(x) + fabs(x);
+}