|
10 | 10 | #include "clang/AST/ASTContext.h"
|
11 | 11 | #include "clang/ASTMatchers/ASTMatchFinder.h"
|
12 | 12 | #include "clang/ASTMatchers/ASTMatchers.h"
|
| 13 | +#include "clang/Basic/TargetInfo.h" |
13 | 14 |
|
14 | 15 | using namespace clang::ast_matchers;
|
15 | 16 |
|
@@ -60,8 +61,45 @@ namespace {
|
60 | 61 | AST_MATCHER(QualType, isVAList) {
|
61 | 62 | ASTContext &Context = Finder->getASTContext();
|
62 | 63 | QualType Desugar = Node.getDesugaredType(Context);
|
63 |
| - return Context.getBuiltinVaListType().getDesugaredType(Context) == Desugar || |
64 |
| - Context.getBuiltinMSVaListType().getDesugaredType(Context) == Desugar; |
| 64 | + QualType NodeTy = Node.getUnqualifiedType(); |
| 65 | + |
| 66 | + auto CheckVaList = [](QualType NodeTy, QualType Expected, |
| 67 | + const ASTContext &Context) { |
| 68 | + if (NodeTy == Expected) |
| 69 | + return true; |
| 70 | + QualType Desugar = NodeTy; |
| 71 | + QualType Ty; |
| 72 | + do { |
| 73 | + Ty = Desugar; |
| 74 | + Desugar = Ty.getSingleStepDesugaredType(Context); |
| 75 | + if (Desugar == Expected) |
| 76 | + return true; |
| 77 | + } while (Desugar != Ty); |
| 78 | + return false; |
| 79 | + }; |
| 80 | + |
| 81 | + // The internal implementation of __builtin_va_list depends on the target |
| 82 | + // type. Some targets implements va_list as 'char *' or 'void *'. |
| 83 | + // In these cases we need to remove all typedefs one by one to check this. |
| 84 | + using BuiltinVaListKind = TargetInfo::BuiltinVaListKind; |
| 85 | + BuiltinVaListKind VaListKind = Context.getTargetInfo().getBuiltinVaListKind(); |
| 86 | + if (VaListKind == BuiltinVaListKind::CharPtrBuiltinVaList || |
| 87 | + VaListKind == BuiltinVaListKind::VoidPtrBuiltinVaList) { |
| 88 | + if (CheckVaList(NodeTy, Context.getBuiltinVaListType(), Context)) |
| 89 | + return true; |
| 90 | + } else if (Desugar == |
| 91 | + Context.getBuiltinVaListType().getDesugaredType(Context)) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + // We also need to check the implementation of __builtin_ms_va_list in the |
| 96 | + // same way, because it may differ from the va_list implementation. |
| 97 | + if (Desugar == Context.getBuiltinMSVaListType().getDesugaredType(Context) && |
| 98 | + CheckVaList(NodeTy, Context.getBuiltinMSVaListType(), Context)) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + return false; |
65 | 103 | }
|
66 | 104 |
|
67 | 105 | AST_MATCHER_P(AdjustedType, hasOriginalType,
|
|
0 commit comments