summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Bytheway <jbytheway@gmail.com>2019-03-07 21:54:51 +0000
committerKim Gräsman <kim.grasman@gmail.com>2019-03-08 08:54:37 +0100
commit33b7ce7500040588771eddac16a21b544c024abf (patch)
tree47f7463d83fc7faf062eb4b7e62b5365d15a1da6
parentca65ee68c286d3e847da0c65ac9005f369edf129 (diff)
Refactor check for builtin function
Create a new function to test whether a decl is a builtin function.
-rw-r--r--iwyu_ast_util.cc10
-rw-r--r--iwyu_ast_util.h4
-rw-r--r--iwyu_output.cc13
3 files changed, 19 insertions, 8 deletions
diff --git a/iwyu_ast_util.cc b/iwyu_ast_util.cc
index 04aaf1c..8f25649 100644
--- a/iwyu_ast_util.cc
+++ b/iwyu_ast_util.cc
@@ -39,6 +39,7 @@
#include "clang/AST/TemplateName.h"
#include "clang/AST/Type.h"
#include "clang/AST/TypeLoc.h"
+#include "clang/Basic/Builtins.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
@@ -1079,6 +1080,15 @@ bool DeclsAreInSameClass(const Decl* decl1, const Decl* decl2) {
return decl1->getDeclContext()->isRecord();
}
+bool IsBuiltinFunction(const clang::NamedDecl* decl,
+ const std::string& symbol_name) {
+ if (const clang::IdentifierInfo* iden = decl->getIdentifier()) {
+ return iden->getBuiltinID() != 0 &&
+ !clang::Builtin::Context::isBuiltinFunc(symbol_name.c_str());
+ }
+ return false;
+}
+
// --- Utilities for Type.
const Type* GetTypeOf(const Expr* expr) {
diff --git a/iwyu_ast_util.h b/iwyu_ast_util.h
index 1993a6d..0785be7 100644
--- a/iwyu_ast_util.h
+++ b/iwyu_ast_util.h
@@ -655,6 +655,10 @@ const clang::NamedDecl* GetNonfriendClassRedecl(const clang::NamedDecl* decl);
// same, and it's a class (or struct).
bool DeclsAreInSameClass(const clang::Decl* decl1, const clang::Decl* decl2);
+// Returns true if the given decl/name is a builtin function
+bool IsBuiltinFunction(const clang::NamedDecl* decl,
+ const std::string& symbol_name);
+
// --- Utilities for Type.
const clang::Type* GetTypeOf(const clang::Expr* expr);
diff --git a/iwyu_output.cc b/iwyu_output.cc
index 4e0066f..1412c5a 100644
--- a/iwyu_output.cc
+++ b/iwyu_output.cc
@@ -1188,14 +1188,11 @@ void ProcessFullUse(OneUse* use,
return;
}
// A compiler builtin without a predefined header file (e.g. __builtin_..)
- if (const clang::IdentifierInfo* iden = use->decl()->getIdentifier()) {
- if (iden->getBuiltinID() != 0 &&
- !clang::Builtin::Context::isBuiltinFunc(use->symbol_name().c_str())) {
- VERRS(6) << "Ignoring use of " << use->symbol_name()
- << " (" << use->PrintableUseLoc() << "): built-in function\n";
- use->set_ignore_use();
- return;
- }
+ if (IsBuiltinFunction(use->decl(), use->symbol_name()) {
+ VERRS(6) << "Ignoring use of " << use->symbol_name()
+ << " (" << use->PrintableUseLoc() << "): built-in function\n";
+ use->set_ignore_use();
+ return;
}
// Special case for operators new/delete: Only treated as built-in if they
// are the default, non-placement versions.