Do not run nullability inference if the input is not in C++
Adding a small check not to run the code for non-C++ files
The same check can also be added in the framework, e.g.
https://github.com/llvm/llvm-project/pull/65301
this patch stops calling it for all functions before then
PiperOrigin-RevId: 562773151
Change-Id: I0b7feb10da4166a4d231282bb31dfd83a91929f4
diff --git a/nullability/inference/BUILD b/nullability/inference/BUILD
index 71556ef..c3fcfec 100644
--- a/nullability/inference/BUILD
+++ b/nullability/inference/BUILD
@@ -105,6 +105,7 @@
":inference_cc_proto",
":merge",
"@llvm-project//clang:ast",
+ "@llvm-project//clang:basic",
"@llvm-project//llvm:Support",
],
)
diff --git a/nullability/inference/infer_tu.cc b/nullability/inference/infer_tu.cc
index 34137ad..9354d14 100644
--- a/nullability/inference/infer_tu.cc
+++ b/nullability/inference/infer_tu.cc
@@ -11,6 +11,7 @@
#include "nullability/inference/inference.proto.h"
#include "nullability/inference/merge.h"
#include "clang/AST/ASTContext.h"
+#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Error.h"
@@ -19,6 +20,16 @@
namespace clang::tidy::nullability {
std::vector<Inference> inferTU(ASTContext& Ctx) {
+ if (!Ctx.getLangOpts().CPlusPlus) {
+ llvm::errs() << "Skipping non-C++ input file: "
+ << Ctx.getSourceManager()
+ .getFileEntryForID(
+ Ctx.getSourceManager().getMainFileID())
+ ->getName()
+ << "\n";
+ return std::vector<Inference>();
+ }
+
std::vector<Evidence> AllEvidence;
// Collect all evidence.
diff --git a/nullability/inference/infer_tu_main.cc b/nullability/inference/infer_tu_main.cc
index 0324222..0946fbe 100644
--- a/nullability/inference/infer_tu_main.cc
+++ b/nullability/inference/infer_tu_main.cc
@@ -133,7 +133,7 @@
llvm::StringRef) override {
class Consumer : public ASTConsumer {
void HandleTranslationUnit(ASTContext &Ctx) override {
- llvm::errs() << "Running inference...";
+ llvm::errs() << "Running inference...\n";
auto Results = inferTU(Ctx);
if (!IncludeTrivial)
llvm::erase_if(Results, [](Inference &I) {