Skip to content

Commit

Permalink
[SYCLomatic][Coverity] Fix converity issues (#2507)
Browse files Browse the repository at this point in the history
Signed-off-by: Ziran Zhang <[email protected]>
  • Loading branch information
ziranzha authored Nov 26, 2024
1 parent 9c1e9db commit a2dc3f5
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 206 deletions.
2 changes: 1 addition & 1 deletion clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ void DpctFileInfo::setFirstIncludeOffset(unsigned Offset) {
if (!HasInclusionDirectiveSet.count(MF)) {
FirstIncludeOffset[MF] = Offset;
LastIncludeOffset = Offset;
HasInclusionDirectiveSet.insert(MF);
HasInclusionDirectiveSet.insert(std::move(MF));
}
}
void DpctFileInfo::concatHeader(llvm::raw_string_ostream &OS) {}
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/DPCT/AnalysisInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,10 @@ class DpctGlobalInfo {
static bool isInAnalysisScope(SourceLocation SL) {
return isInAnalysisScope(DpctGlobalInfo::getLocInfo(SL).first);
}
static bool isInAnalysisScope(clang::tooling::UnifiedPath FilePath) {
static bool isInAnalysisScope(const clang::tooling::UnifiedPath &FilePath) {
return std::any_of(AnalysisScope.begin(), AnalysisScope.end(),
[FP = FilePath](const clang::tooling::UnifiedPath &P) {
return isChildPath(P, FP);
[&](const clang::tooling::UnifiedPath &P) {
return isChildPath(P, FilePath);
});
}
static bool isExcluded(const clang::tooling::UnifiedPath &FilePath);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/DPCT/CommandOption/ValidateArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void validateInputDirectory(UnifiedPath InRootPath) {
dpctExit(MigrationErrorInputDirContainSDKFolder);
}

if (isChildOrSamePath(InRootPath, DpctInstallPath)) {
if (isChildOrSamePath(std::move(InRootPath), DpctInstallPath)) {
ShowStatus(MigrationErrorInputDirContainCTTool);
dpctExit(MigrationErrorInputDirContainCTTool);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/DPCT/DPCT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ void callIndependentToolAndExit(const std::string IndependentTool, int argc, con
}
int ProcessExitCode = system(SystemCallCommand.c_str());
if (ProcessExitCode) {
ShowStatus(CallIndependentToolError, IndependentTool);
ShowStatus(CallIndependentToolError, std::move(IndependentTool));
dpctExit(CallIndependentToolError);
}
dpctExit(CallIndependentToolSucceeded);
Expand Down
14 changes: 7 additions & 7 deletions clang/lib/DPCT/FileGenerator/GenFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void processAllFiles(StringRef InRoot, StringRef OutRoot,
" fail: " + EC.message() + "\n";
PrintMsg(ErrMsg);
}
auto FilePath = Iter->path();
auto &FilePath = Iter->path();

// Skip output directory if it is in the in-root directory.
if (isChildOrSamePath(OutRoot.str(), FilePath))
Expand Down Expand Up @@ -419,16 +419,16 @@ void processAllFiles(StringRef InRoot, StringRef OutRoot,
}
if (GetSourceFileType(FilePath) & SPT_CudaSource) {
// Only migrates isolated CUDA source files.
FilesNotProcessed.push_back(FilePath);
FilesNotProcessed.push_back(std::move(FilePath));
} else {
// Collect the rest files which are not in the compilation database or
// included by main source file in the compilation database.
FilesNotInCompilationDB.push_back(FilePath);
FilesNotInCompilationDB.push_back(std::move(FilePath));
}
}

} else if (Iter->type() == fs::file_type::directory_file) {
const auto Path = Iter->path();
const auto &Path = Iter->path();
clang::tooling::UnifiedPath OutDirectory = Path;
if (!rewriteCanonicalDir(OutDirectory, InRoot, OutRoot)) {
continue;
Expand Down Expand Up @@ -529,10 +529,10 @@ int writeReplacementsToFiles(
OutPath = StringRef(DpctGlobalInfo::removeSymlinks(
Rewrite.getSourceMgr().getFileManager(), Entry.first));
bool HasRealReplacements = true;
auto Repls = Entry.second;
const auto &Repls = Entry.second;

if (Repls.size() == 1) {
auto Repl = *Repls.begin();
const auto &Repl = *Repls.begin();
if (Repl.getLength() == 0 && Repl.getReplacementText().empty())
HasRealReplacements = false;
}
Expand Down Expand Up @@ -1226,7 +1226,7 @@ void loadYAMLIntoFileInfo(clang::tooling::UnifiedPath Path) {
if (clang::dpct::DpctGlobalInfo::isIncMigration()) {
if (loadFromYaml(YamlFilePath, *PreTU) == 0) {
DpctGlobalInfo::getInstance().insertReplInfoFromYAMLToFileInfo(
OriginPath, PreTU);
OriginPath, std::move(PreTU));
} else {
llvm::errs() << getLoadYamlFailWarning(YamlFilePath);
}
Expand Down
19 changes: 10 additions & 9 deletions clang/lib/DPCT/MigrateScript/GenMakefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static void getCompileInfo(
for (auto &Obj : ObjsInLKOrARCmd) {
ObjsInLinkerCmdPerTarget[TargetName].push_back(Obj);
}
ToolPerTarget[TargetName] = Tool;
ToolPerTarget[TargetName] = std::move(Tool);
} else {
continue;
}
Expand Down Expand Up @@ -342,7 +342,7 @@ static void getCompileInfo(
IsObjSpecified = true;
} else if (IsObjName) {
clang::tooling::UnifiedPath FilePathAbs(Option, Directory);
Orig2ObjMap[FileName] = FilePathAbs;
Orig2ObjMap[FileName] = std::move(FilePathAbs);
IsObjName = false;
} else if (llvm::StringRef(Option).starts_with("-O")) {
// Keep optimization level same as original compile command.
Expand Down Expand Up @@ -377,26 +377,27 @@ static void getCompileInfo(
// needs to be a existing file path passed to DpctFileInfo referred in
// rewriteFileName() to avoid potential crash issue.
rewriteFileName(FileName);
rewriteCanonicalDir(FileName, InRoot, OutRoot);
if (!rewriteCanonicalDir(FileName, InRoot, OutRoot))
continue;

if (llvm::sys::fs::exists(FileName.getCanonicalPath())) {
SmallString<512> OutDirectory(FileName.getCanonicalPath());
llvm::sys::path::replace_path_prefix(OutDirectory,
OutRoot.getCanonicalPath(), ".");
clang::tooling::CompilationInfo CmpInfo;
CmpInfo.MigratedFileName = OutDirectory.c_str();
CmpInfo.CompileOptions = NewOptions;
CmpInfo.CompileOptions = std::move(NewOptions);
CmpInfo.Compiler = Entry.second[1];
CmdsMap[Orig2ObjMap[OrigFileName]] = CmpInfo;
CmdsMap[Orig2ObjMap[OrigFileName]] = std::move(CmpInfo);
} else {
SmallString<512> OutDirectory(OrigFileName.getCanonicalPath());
llvm::sys::path::replace_path_prefix(OutDirectory,
InRoot.getCanonicalPath(), ".");
clang::tooling::CompilationInfo CmpInfo;
CmpInfo.MigratedFileName = OutDirectory.c_str();
CmpInfo.CompileOptions = NewOptions;
CmpInfo.CompileOptions = std::move(NewOptions);
CmpInfo.Compiler = Entry.second[1];
CmdsMap[Orig2ObjMap[OrigFileName]] = CmpInfo;
CmdsMap[Orig2ObjMap[OrigFileName]] = std::move(CmpInfo);
}
}

Expand Down Expand Up @@ -593,7 +594,7 @@ static void genMakefile(

SmallString<512> Source = StringRef(Entry.second[Idx].MigratedFileName);

auto Option = Entry.second[Idx].CompileOptions;
auto &Option = Entry.second[Idx].CompileOptions;
SmallString<512> Obj = StringRef(Source);
path::replace_extension(Obj, "o");

Expand Down Expand Up @@ -676,7 +677,7 @@ void genBuildScript(clang::tooling::RefactoringTool &Tool,
}

if (!NeedMergetYaml)
CompileCmdsPerTarget = NewCompileCmdsMap;
CompileCmdsPerTarget = std::move(NewCompileCmdsMap);

genMakefile(Tool, OutRoot, BuildScriptName, CompileCmdsPerTarget,
ToolPerTarget);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/DPCT/MigrateScript/MigrateBuildScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ getCmakeBuildPathFromInRoot(const clang::tooling::UnifiedPath &InRoot,
if (fs::exists(appendPath(Path.getCanonicalPath().str(), "CMakeFiles")) &&
fs::exists(
appendPath(Path.getCanonicalPath().str(), "CMakeCache.txt"))) {
CmakeBuildDirectory = Path;
CmakeBuildDirectory = std::move(Path);
break;
}
}
Expand Down
19 changes: 8 additions & 11 deletions clang/lib/DPCT/MigrateScript/MigrateCmakeScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ std::string getVarName(const std::string &Variable) {
auto Name = Variable.substr(2, Variable.size() - 3);

auto Iter = CmakeVarMap.find(Name);
if (CmakeVarMap.find(Name) != CmakeVarMap.end()) {
if (Iter != CmakeVarMap.end()) {
Value = Iter->second;
}
} else {
Expand Down Expand Up @@ -153,7 +153,6 @@ static void parseVariable(const std::string &Input) {
if (Command == "set") {
Index++; // Skip '('
std::string VarName;
std::string Value;

// Get the begin of first argument of set
Index = skipWhiteSpaces(Input, Index);
Expand All @@ -176,16 +175,14 @@ static void parseVariable(const std::string &Input) {
Index = gotoEndOfCmakeWord(Input, Begin + 1, ')');
End = Index;

// Get the name of the second argument
Value = Input.substr(Begin, End - Begin);

// Only check the set commands which has two arguments.
// And skip cmake reserves identifiers that begins with "CMAKE_" and
// "_CMAKE_".
if (Input[Index] == ')' &&
!llvm::StringRef(VarName).starts_with("CMAKE_") &&
!llvm::StringRef(VarName).starts_with("_CMAKE_ ")) {
CmakeVarMap[VarName] = Value;
// Get the name of the second argument
CmakeVarMap[VarName] = Input.substr(Begin, End - Begin);
}
}

Expand Down Expand Up @@ -216,15 +213,15 @@ static std::string processArgOfCmakeVersionRequired(
std::atof(MaxVer.c_str()) > std::atof(CmakeMinVerion.c_str())) {
ReplArg = CmakeMinVerion + "..." + MaxVer;
} else {
ReplArg = CmakeMinVerion;
ReplArg = std::move(CmakeMinVerion);
}

} else {
std::string Ver = getVarName(Arg);
if (std::atof(Ver.c_str()) < std::atof(CmakeMinVerion.c_str())) {
ReplArg = CmakeMinVerion;
ReplArg = std::move(CmakeMinVerion);
} else {
ReplArg = Ver;
ReplArg = std::move(Ver);
}
}
return ReplArg;
Expand Down Expand Up @@ -403,7 +400,7 @@ static std::string convertCmakeCommandsToLower(const std::string &InputString,
WarningMsg += DiagnosticsUtils::getMsgText(
CMakeScriptMigrationMsgs::CMAKE_CONFIG_FILE_WARNING, Str);
WarningMsg += "\n";
FileWarningsMap[FileName].push_back(WarningMsg);
FileWarningsMap[FileName].push_back(std::move(WarningMsg));

OutputStream
<< "# "
Expand All @@ -418,7 +415,7 @@ static std::string convertCmakeCommandsToLower(const std::string &InputString,
WarningMsg += DiagnosticsUtils::getMsgText(
CMakeScriptMigrationMsgs::CMAKE_NOT_SUPPORT_WARNING, Str);
WarningMsg += "\n";
FileWarningsMap[FileName].push_back(WarningMsg);
FileWarningsMap[FileName].push_back(std::move(WarningMsg));

OutputStream
<< "# "
Expand Down
36 changes: 19 additions & 17 deletions clang/lib/DPCT/PreProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,24 +451,26 @@ void IncludesCallbacks::MacroExpands(const Token &MacroNameTok,
? (arg1)*1000 + (arg2)*100 + (arg3)
: (arg1)*10000 + (arg2)*100 + (arg3));
};
for (unsigned int i = 0; i < Args->getNumMacroArguments(); ++i) {
MA[i] =
Lexer::getSourceText(CharSourceRange::getCharRange(
Args->getUnexpArgument(i)->getLocation(),
Lexer::getLocForEndOfToken(
Args->getUnexpArgument(i)->getLocation(),
0, SM, LangOptions())),
SM, LangOptions())
.str();
if (Args) {
for (unsigned int i = 0; i < Args->getNumMacroArguments(); ++i) {
MA[i] = Lexer::getSourceText(
CharSourceRange::getCharRange(
Args->getUnexpArgument(i)->getLocation(),
Lexer::getLocForEndOfToken(
Args->getUnexpArgument(i)->getLocation(), 0, SM,
LangOptions())),
SM, LangOptions())
.str();
}
auto Length = Lexer::MeasureTokenLength(
Range.getEnd(), SM, dpct::DpctGlobalInfo::getContext().getLangOpts());
Length += SM.getDecomposedLoc(Range.getEnd()).second -
SM.getDecomposedLoc(Range.getBegin()).second;
TransformSet.emplace_back(new ReplaceText(
Range.getBegin(), Length,
std::move(calCclCompatVersion(std::stoi(MA[0]), std::stoi(MA[1]),
std::stoi(MA[2])))));
}
auto Length = Lexer::MeasureTokenLength(
Range.getEnd(), SM, dpct::DpctGlobalInfo::getContext().getLangOpts());
Length += SM.getDecomposedLoc(Range.getEnd()).second -
SM.getDecomposedLoc(Range.getBegin()).second;
TransformSet.emplace_back(new ReplaceText(
Range.getBegin(), Length,
std::move(calCclCompatVersion(std::stoi(MA[0]), std::stoi(MA[1]),
std::stoi(MA[2])))));
}

if (Name == "NCCL_MAJOR" || Name == "NCCL_MINOR") {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/DPCT/RuleInfra/TypeLocRewriters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ makeTemplateArgCreator(unsigned Idx) {
std::function<std::string(const TypeLoc)>
makeUserDefinedTypeStrCreator(MetaRuleObject &R,
std::shared_ptr<TypeOutputBuilder> TOB) {
return [&R, TOB](const TypeLoc TL) {
return [&R, Builder = std::move(TOB)](const TypeLoc TL) {
if (!TL)
return std::string();
auto Range = getDefinitionRange(TL.getBeginLoc(), TL.getEndLoc());
Expand All @@ -80,7 +80,7 @@ makeUserDefinedTypeStrCreator(MetaRuleObject &R,

std::string ResultStr;
llvm::raw_string_ostream OS(ResultStr);
for (auto &tob : TOB->SubBuilders) {
for (auto &tob : Builder->SubBuilders) {
switch (tob->Kind) {
case (OutputBuilder::Kind::String):
OS << tob->Str;
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/DPCT/RulesLang/RulesLang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ static const CXXConstructorDecl *getIfConstructorDecl(const Decl *ND) {
static internal::Matcher<NamedDecl> vectorTypeName() {
std::vector<std::string> TypeNames(MapNamesLang::SupportedVectorTypes.begin(),
MapNamesLang::SupportedVectorTypes.end());
return internal::Matcher<NamedDecl>(new internal::HasNameMatcher(TypeNames));
return internal::Matcher<NamedDecl>(
new internal::HasNameMatcher(std::move(TypeNames)));
}

void ErrorHandlingHostAPIRule::registerMatcher(MatchFinder &MF) {
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/DPCT/RulesLang/RulesLangCooperativeGroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ void CooperativeGroupsFunctionRule::runRule(
FunctionName);
}
}
ReportUnsupportedWarning(const ReportUnsupportedWarning &) = delete;
ReportUnsupportedWarning &
operator=(const ReportUnsupportedWarning &) = delete;
bool NeedReport = true;

private:
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/DPCT/RulesLang/RulesLangTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,8 @@ bool TextureRule::SettersMerger::applyResult() {
ResultMap[S] = false;
}
}
ResultMapInserter(const ResultMapInserter &) = delete;
ResultMapInserter &operator=(const ResultMapInserter &) = delete;
void update(size_t Index, const Stmt *S) {
auto &Latest = LatestStmts[Index];
if (Latest)
Expand Down
Loading

0 comments on commit a2dc3f5

Please sign in to comment.