Skip to content

Commit

Permalink
Implement basic type lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Endre Fülöp committed Nov 10, 2022
1 parent faffbca commit eb1c93d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
72 changes: 43 additions & 29 deletions clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1457,48 +1457,62 @@ std::string Option{"Config"};
Mgr->getAnalyzerOptions().getCheckerStringOption(this, Option);*/
StringRef ConfigFile = "/local/workspace/llvm-project/clang/test/Analysis/Inputs/fread-summary.yaml";
llvm::Optional<SummaryConfiguration> Config =
getConfiguration<SummaryConfiguration>(*Mgr, this, Option, ConfigFile);
llvm::errs()<<"Config :"<<Config.hasValue()<<"\n";
if (Config.has_value()){
for (const SummaryConfiguration::Summary &s : Config->summaries){
llvm::errs()<<"Config :"<<s.name<<"\n";

ArgTypes args;
for (const std::string &t: s.signature.argTypes){
auto ltype = lookupTy(t);
if (ltype.has_value()){
llvm::errs()<<"type string:"<<t<<"\n";
ltype->dump();
args.push_back(lookupTy(t));
}
getConfiguration<SummaryConfiguration>(*Mgr, this, Option, ConfigFile);
llvm::errs() << "Config :" << Config.has_value() << "\n";

auto GetTypeFromStr = [&](StringRef TypeName) {
Optional<QualType> LType = lookupTy(TypeName);
if (LType)
return *LType;

return llvm::StringSwitch<QualType>(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);
case SummaryConfiguration::EvaluationType::EvalCallAsPure:
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);
}
}
/*
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Analysis/Inputs/fread-summary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eb1c93d

Please sign in to comment.