From d256712143eb1181cc472a431707424eabb62792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?= Date: Thu, 10 Nov 2022 17:04:16 +0100 Subject: [PATCH] Implement basic type lookup --- .../Checkers/StdLibraryFunctionsChecker.cpp | 72 +++++++++++-------- clang/test/Analysis/Inputs/fread-summary.yaml | 4 +- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index f397070e652b05..92315b1b6b3bde 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -1457,23 +1457,39 @@ std::string Option{"Config"}; Mgr->getAnalyzerOptions().getCheckerStringOption(this, Option);*/ StringRef ConfigFile = "/local/workspace/llvm-project/clang/test/Analysis/Inputs/fread-summary.yaml"; llvm::Optional Config = - getConfiguration(*Mgr, this, Option, ConfigFile); -llvm::errs()<<"Config :"<summaries){ - llvm::errs()<<"Config :"<dump(); - args.push_back(lookupTy(t)); - } + getConfiguration(*Mgr, this, Option, ConfigFile); +llvm::errs() << "Config :" << Config.has_value() << "\n"; + +auto GetTypeFromStr = [&](StringRef TypeName) { + Optional LType = lookupTy(TypeName); + if (LType) + return *LType; + + return llvm::StringSwitch(TypeName) + .Case("void *", VoidPtrTy) + .Case("void * restrict", VoidPtrRestrictTy) + .Default(Irrelevant); +}; + +if (Config.has_value()) { + for (const SummaryConfiguration::Summary &s : Config->summaries) { + ArgTypes Args; + for (const std::string &TypeName : s.signature.argTypes) { + llvm::errs() << "arg type string:" << TypeName << "\n"; + QualType Type = GetTypeFromStr(TypeName); + llvm::errs() << "arg type dump:"; + Type.dump(); + llvm::errs() << "\n"; + + Args.push_back(Type); } - RetType rt = lookupTy(s.signature.returnType); - auto GetSummary = [s]() { + + const std::string &RetTypeName = s.signature.returnType; + llvm::errs() << "ret type string:" << RetTypeName << "\n"; + QualType RetType = GetTypeFromStr(RetTypeName); + llvm::errs() << "ret type dump:"; + + auto GetSummary = [&s]() { switch (s.evaluationType) { case SummaryConfiguration::EvaluationType::NoEvalCall: return Summary(NoEvalCall); @@ -1481,24 +1497,22 @@ if (Config.has_value()){ return Summary(EvalCallAsPure); } }; + Summary summary = GetSummary(); - for (const SummaryConfiguration::ArgConstraint &ac: s.argConstraints){ - switch (ac.type){ - case SummaryConfiguration::ArgConstraintType::NotNull: - summary.ArgConstraint(NotNull(ac.arg)); - break; - case SummaryConfiguration::ArgConstraintType::BufferSize: - summary.ArgConstraint(BufferSize(ac.bufferArg,ac.sizeArg, ac.countArg)); - break; + for (const SummaryConfiguration::ArgConstraint &AC : s.argConstraints) { + switch (AC.type) { + case SummaryConfiguration::ArgConstraintType::NotNull: + summary.ArgConstraint(NotNull(AC.arg)); + break; + case SummaryConfiguration::ArgConstraintType::BufferSize: + summary.ArgConstraint( + BufferSize(AC.bufferArg, AC.sizeArg, AC.countArg)); + break; } } - addToFunctionSummaryMap( - s.name, - Signature(args, - rt), - summary); + addToFunctionSummaryMap(s.name, Signature(Args, RetType), summary); } } /* diff --git a/clang/test/Analysis/Inputs/fread-summary.yaml b/clang/test/Analysis/Inputs/fread-summary.yaml index 02a880a1078f53..0c7b582e886a52 100644 --- a/clang/test/Analysis/Inputs/fread-summary.yaml +++ b/clang/test/Analysis/Inputs/fread-summary.yaml @@ -4,10 +4,10 @@ Summaries: - Name: "fread" Signature: ArgTypes: + - "void*" - "size_t" - "size_t" - - "size_t" - - "FILE *restrict" + - "FILE*" RetType: "size_t" EvaluationType: "NoEvalCall" ArgConstraints: # We give an error if this is violated