llvm-project
213 строк · 7.7 Кб
1// TODO: header template
2
3#include "clang/AST/OSLog.h"4#include "clang/AST/Attr.h"5#include "clang/AST/Decl.h"6#include "clang/AST/DeclCXX.h"7#include "clang/AST/ExprObjC.h"8#include "clang/AST/FormatString.h"9#include "clang/Basic/Builtins.h"10#include "llvm/ADT/SmallBitVector.h"11#include <optional>12
13using namespace clang;14
15using clang::analyze_os_log::OSLogBufferItem;16using clang::analyze_os_log::OSLogBufferLayout;17
18namespace {19class OSLogFormatStringHandler20: public analyze_format_string::FormatStringHandler {21private:22struct ArgData {23const Expr *E = nullptr;24std::optional<OSLogBufferItem::Kind> Kind;25std::optional<unsigned> Size;26std::optional<const Expr *> Count;27std::optional<const Expr *> Precision;28std::optional<const Expr *> FieldWidth;29unsigned char Flags = 0;30StringRef MaskType;31};32SmallVector<ArgData, 4> ArgsData;33ArrayRef<const Expr *> Args;34
35OSLogBufferItem::Kind36getKind(analyze_format_string::ConversionSpecifier::Kind K) {37switch (K) {38case clang::analyze_format_string::ConversionSpecifier::sArg: // "%s"39return OSLogBufferItem::StringKind;40case clang::analyze_format_string::ConversionSpecifier::SArg: // "%S"41return OSLogBufferItem::WideStringKind;42case clang::analyze_format_string::ConversionSpecifier::PArg: { // "%P"43return OSLogBufferItem::PointerKind;44case clang::analyze_format_string::ConversionSpecifier::ObjCObjArg: // "%@"45return OSLogBufferItem::ObjCObjKind;46case clang::analyze_format_string::ConversionSpecifier::PrintErrno: // "%m"47return OSLogBufferItem::ErrnoKind;48default:49return OSLogBufferItem::ScalarKind;50}51}52}53
54public:55OSLogFormatStringHandler(ArrayRef<const Expr *> Args) : Args(Args) {56ArgsData.reserve(Args.size());57}58
59bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,60const char *StartSpecifier, unsigned SpecifierLen,61const TargetInfo &) override {62if (!FS.consumesDataArgument() &&63FS.getConversionSpecifier().getKind() !=64clang::analyze_format_string::ConversionSpecifier::PrintErrno)65return true;66
67ArgsData.emplace_back();68unsigned ArgIndex = FS.getArgIndex();69if (ArgIndex < Args.size())70ArgsData.back().E = Args[ArgIndex];71
72// First get the Kind73ArgsData.back().Kind = getKind(FS.getConversionSpecifier().getKind());74if (ArgsData.back().Kind != OSLogBufferItem::ErrnoKind &&75!ArgsData.back().E) {76// missing argument77ArgsData.pop_back();78return false;79}80
81switch (FS.getConversionSpecifier().getKind()) {82case clang::analyze_format_string::ConversionSpecifier::sArg: // "%s"83case clang::analyze_format_string::ConversionSpecifier::SArg: { // "%S"84auto &precision = FS.getPrecision();85switch (precision.getHowSpecified()) {86case clang::analyze_format_string::OptionalAmount::NotSpecified: // "%s"87break;88case clang::analyze_format_string::OptionalAmount::Constant: // "%.16s"89ArgsData.back().Size = precision.getConstantAmount();90break;91case clang::analyze_format_string::OptionalAmount::Arg: // "%.*s"92ArgsData.back().Count = Args[precision.getArgIndex()];93break;94case clang::analyze_format_string::OptionalAmount::Invalid:95return false;96}97break;98}99case clang::analyze_format_string::ConversionSpecifier::PArg: { // "%P"100auto &precision = FS.getPrecision();101switch (precision.getHowSpecified()) {102case clang::analyze_format_string::OptionalAmount::NotSpecified: // "%P"103return false; // length must be supplied with pointer format specifier104case clang::analyze_format_string::OptionalAmount::Constant: // "%.16P"105ArgsData.back().Size = precision.getConstantAmount();106break;107case clang::analyze_format_string::OptionalAmount::Arg: // "%.*P"108ArgsData.back().Count = Args[precision.getArgIndex()];109break;110case clang::analyze_format_string::OptionalAmount::Invalid:111return false;112}113break;114}115default:116if (FS.getPrecision().hasDataArgument()) {117ArgsData.back().Precision = Args[FS.getPrecision().getArgIndex()];118}119break;120}121if (FS.getFieldWidth().hasDataArgument()) {122ArgsData.back().FieldWidth = Args[FS.getFieldWidth().getArgIndex()];123}124
125if (FS.isSensitive())126ArgsData.back().Flags |= OSLogBufferItem::IsSensitive;127else if (FS.isPrivate())128ArgsData.back().Flags |= OSLogBufferItem::IsPrivate;129else if (FS.isPublic())130ArgsData.back().Flags |= OSLogBufferItem::IsPublic;131
132ArgsData.back().MaskType = FS.getMaskType();133return true;134}135
136void computeLayout(ASTContext &Ctx, OSLogBufferLayout &Layout) const {137Layout.Items.clear();138for (auto &Data : ArgsData) {139if (!Data.MaskType.empty()) {140CharUnits Size = CharUnits::fromQuantity(8);141Layout.Items.emplace_back(OSLogBufferItem::MaskKind, nullptr,142Size, 0, Data.MaskType);143}144
145if (Data.FieldWidth) {146CharUnits Size = Ctx.getTypeSizeInChars((*Data.FieldWidth)->getType());147Layout.Items.emplace_back(OSLogBufferItem::ScalarKind, *Data.FieldWidth,148Size, 0);149}150if (Data.Precision) {151CharUnits Size = Ctx.getTypeSizeInChars((*Data.Precision)->getType());152Layout.Items.emplace_back(OSLogBufferItem::ScalarKind, *Data.Precision,153Size, 0);154}155if (Data.Count) {156// "%.*P" has an extra "count" that we insert before the argument.157CharUnits Size = Ctx.getTypeSizeInChars((*Data.Count)->getType());158Layout.Items.emplace_back(OSLogBufferItem::CountKind, *Data.Count, Size,1590);160}161if (Data.Size)162Layout.Items.emplace_back(Ctx, CharUnits::fromQuantity(*Data.Size),163Data.Flags);164if (Data.Kind) {165CharUnits Size;166if (*Data.Kind == OSLogBufferItem::ErrnoKind)167Size = CharUnits::Zero();168else169Size = Ctx.getTypeSizeInChars(Data.E->getType());170Layout.Items.emplace_back(*Data.Kind, Data.E, Size, Data.Flags);171} else {172auto Size = Ctx.getTypeSizeInChars(Data.E->getType());173Layout.Items.emplace_back(OSLogBufferItem::ScalarKind, Data.E, Size,174Data.Flags);175}176}177}178};179} // end anonymous namespace180
181bool clang::analyze_os_log::computeOSLogBufferLayout(182ASTContext &Ctx, const CallExpr *E, OSLogBufferLayout &Layout) {183ArrayRef<const Expr *> Args(E->getArgs(), E->getArgs() + E->getNumArgs());184
185const Expr *StringArg;186ArrayRef<const Expr *> VarArgs;187switch (E->getBuiltinCallee()) {188case Builtin::BI__builtin_os_log_format_buffer_size:189assert(E->getNumArgs() >= 1 &&190"__builtin_os_log_format_buffer_size takes at least 1 argument");191StringArg = E->getArg(0);192VarArgs = Args.slice(1);193break;194case Builtin::BI__builtin_os_log_format:195assert(E->getNumArgs() >= 2 &&196"__builtin_os_log_format takes at least 2 arguments");197StringArg = E->getArg(1);198VarArgs = Args.slice(2);199break;200default:201llvm_unreachable("non-os_log builtin passed to computeOSLogBufferLayout");202}203
204const StringLiteral *Lit = cast<StringLiteral>(StringArg->IgnoreParenCasts());205assert(Lit && (Lit->isOrdinary() || Lit->isUTF8()));206StringRef Data = Lit->getString();207OSLogFormatStringHandler H(VarArgs);208ParsePrintfString(H, Data.begin(), Data.end(), Ctx.getLangOpts(),209Ctx.getTargetInfo(), /*isFreeBSDKPrintf*/ false);210
211H.computeLayout(Ctx, Layout);212return true;213}
214