[clang-tools-extra] Add new check: do not return 0; at the end of main() in C++ (PR #77586)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 10 03:47:18 PST 2024


================
@@ -0,0 +1,50 @@
+//===--- DonotreturnzerocheckCheck.cpp - clang-tidy -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "DonotreturnzerocheckCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+bool isCPlusPlusOrC99(const LangOptions &LangOpts) {
+  return LangOpts.CPlusPlus || LangOpts.C99;
+}
+
+void DonotreturnzerocheckCheck::registerMatchers(MatchFinder *Finder) {
+  // FIXME: Add matchers.
+  Finder->addMatcher(
+      functionDecl(isMain(), returns(asString("int"))).bind("main"), this);
+}
+
+void DonotreturnzerocheckCheck::check(const MatchFinder::MatchResult &Result) {
+  // FIXME: Add callback implementation.
+  const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("main");
+  if (isCPlusPlusOrC99(Result.Context->getLangOpts())) {
----------------
HerrCai0907 wrote:

move it to `bool isLanguageVersionSupported(const LangOptions &LangOpts) const override`

https://github.com/llvm/llvm-project/pull/77586


More information about the cfe-commits mailing list