[PATCH] D98438: Clang: Allow selecting the hash algorithm for file checksums in debug info.

2022-09-23 Thread Arlo Siemsen via Phabricator via cfe-commits
arlosi abandoned this revision.
arlosi added a comment.

Closing in favor of D134544 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98438/new/

https://reviews.llvm.org/D98438

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116835: Use sha256 for source file debug info checksums with MSVC compat >= 1930

2022-01-07 Thread Arlo Siemsen via Phabricator via cfe-commits
arlosi added a comment.

I submitted a change last year to hook up the /ZH option, but it was never 
completed. https://reviews.llvm.org/D98438


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116835/new/

https://reviews.llvm.org/D116835

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D98438: Clang: Allow selecting the hash algorithm for file checksums in debug info.

2021-03-22 Thread Arlo Siemsen via Phabricator via cfe-commits
arlosi added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98438/new/

https://reviews.llvm.org/D98438

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D98438: Clang: Allow selecting the hash algorithm for file checksums in debug info.

2021-03-12 Thread Arlo Siemsen via Phabricator via cfe-commits
arlosi updated this revision to Diff 330386.
arlosi marked an inline comment as done.
arlosi added a comment.

Use automatic marshaling for command line arguments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98438/new/

https://reviews.llvm.org/D98438

Files:
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Basic/CodeGenOptions.h
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/Driver/ToolChains/Clang.cpp

Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -7107,6 +7107,19 @@
   D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs;
 }
   }
+
+  if (Arg *A = Args.getLastArg(options::OPT__SLASH_ZH)) {
+StringRef Val = A->getValue();
+if (Val.equals("MD5")) {
+  CmdArgs.push_back("-gsrc-hash-algorithm=md5");
+} else if (Val.equals("SHA1")) {
+  CmdArgs.push_back("-gsrc-hash-algorithm=sha1");
+} else if (Val.equals("SHA_256")) {
+  CmdArgs.push_back("-gsrc-hash-algorithm=sha256");
+} else {
+  D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
+}
+  }
 }
 
 const char *Clang::getBaseInputName(const ArgList ,
Index: clang/lib/CodeGen/CGDebugInfo.h
===
--- clang/lib/CodeGen/CGDebugInfo.h
+++ clang/lib/CodeGen/CGDebugInfo.h
@@ -572,8 +572,9 @@
   void CreateCompileUnit();
 
   /// Compute the file checksum debug info for input file ID.
-  Optional
-  computeChecksum(FileID FID, SmallString<32> ) const;
+  /// Storage of the checksum string is owned by the caller.
+  Optional>
+  computeChecksum(FileID FID, SmallString<64> ) const;
 
   /// Get the source of the given file ID.
   Optional getSource(const SourceManager , FileID FID);
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -44,9 +44,13 @@
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/SHA1.h"
+#include "llvm/Support/SHA256.h"
 #include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/raw_ostream.h"
 using namespace clang;
 using namespace clang::CodeGen;
 
@@ -361,10 +365,12 @@
   return StringRef();
 }
 
-Optional
-CGDebugInfo::computeChecksum(FileID FID, SmallString<32> ) const {
+Optional>
+CGDebugInfo::computeChecksum(FileID FID, SmallString<64> ) const {
   Checksum.clear();
 
+  llvm::DIFile::ChecksumKind CSKind;
+  Optional> CSInfo;
   if (!CGM.getCodeGenOpts().EmitCodeView &&
   CGM.getCodeGenOpts().DwarfVersion < 5)
 return None;
@@ -374,14 +380,40 @@
   if (!MemBuffer)
 return None;
 
-  llvm::MD5 Hash;
-  llvm::MD5::MD5Result Result;
-
-  Hash.update(MemBuffer->getBuffer());
-  Hash.final(Result);
+  switch (CGM.getCodeGenOpts().getDebugSrcHashAlgorithm()) {
+  case clang::CodeGenOptions::CSK_MD5: {
+CSKind = llvm::DIFile::CSK_MD5;
+llvm::MD5 Hash;
+llvm::MD5::MD5Result Result;
+Hash.update(MemBuffer->getBuffer());
+Hash.final(Result);
+Checksum = Result.digest();
+break;
+  }
+  case clang::CodeGenOptions::CSK_SHA1: {
+CSKind = llvm::DIFile::CSK_SHA1;
+llvm::SHA1 Hash;
+Hash.update(MemBuffer->getBuffer());
+StringRef Result = Hash.final();
+llvm::raw_svector_ostream Res(Checksum);
+for (int i = 0; i < 20; ++i)
+  Res << llvm::format("%.2x", static_cast(Result[i]));
+break;
+  }
+  case clang::CodeGenOptions::CSK_SHA256: {
+CSKind = llvm::DIFile::CSK_SHA256;
+llvm::SHA256 Hash;
+Hash.update(MemBuffer->getBuffer());
+StringRef Result = Hash.final();
+llvm::raw_svector_ostream Res(Checksum);
+for (int i = 0; i < 32; ++i)
+  Res << llvm::format("%.2x", static_cast(Result[i]));
+break;
+  }
+  }
 
-  Hash.stringifyResult(Result, Checksum);
-  return llvm::DIFile::CSK_MD5;
+  CSInfo.emplace(CSKind, Checksum);
+  return CSInfo;
 }
 
 Optional CGDebugInfo::getSource(const SourceManager ,
@@ -428,12 +460,10 @@
   return cast(V);
   }
 
-  SmallString<32> Checksum;
+  SmallString<64> Checksum;
+  Optional> CSInfo =
+  computeChecksum(FID, Checksum);
 
-  Optional CSKind = computeChecksum(FID, Checksum);
-  Optional> CSInfo;
-  if (CSKind)
-CSInfo.emplace(*CSKind, Checksum);
   return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc)));
 }
 
@@ -519,8 +549,7 @@
 }
 
 void CGDebugInfo::CreateCompileUnit() {
-  SmallString<32> Checksum;
-  Optional CSKind;
+  SmallString<64> Checksum;
   Optional> CSInfo;
 
   // Should we be asking the SourceManager for the main file name, instead of
@@ 

[PATCH] D98438: Clang: Allow selecting the hash algorithm for file checksums in debug info.

2021-03-11 Thread Arlo Siemsen via Phabricator via cfe-commits
arlosi created this revision.
Herald added subscribers: jansvoboda11, dexonsmith, dang.
arlosi requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Adds clang-cl support for the /ZH: option used to select MD5, SHA1,
or SHA_256 hashing algorithms in debug info.

Previously only the MD5 algorithm was supported.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98438

Files:
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Basic/CodeGenOptions.h
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp

Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -1928,6 +1928,20 @@
   else if (Args.hasArg(options::OPT_fno_finite_loops))
 Opts.FiniteLoops = CodeGenOptions::FiniteLoopsKind::Never;
 
+  if (Arg *A = Args.getLastArg(OPT_gsrc_hash_algorithm_EQ)) {
+unsigned Val = llvm::StringSwitch(A->getValue())
+   .Case("md5", CodeGenOptions::CSK_MD5)
+   .Case("sha1", CodeGenOptions::CSK_SHA1)
+   .Case("sha256", CodeGenOptions::CSK_SHA256)
+   .Default(~0U);
+if (Val == ~0U)
+  Diags.Report(diag::err_drv_invalid_value)
+  << A->getAsString(Args) << A->getValue();
+else
+  Opts.setDebugSrcHashAlgorithm(
+  static_cast(Val));
+  }
+
   return Success && Diags.getNumErrors() == NumErrorsBefore;
 }
 
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -7107,6 +7107,19 @@
   D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << GuardArgs;
 }
   }
+
+  if (Arg *A = Args.getLastArg(options::OPT__SLASH_ZH)) {
+StringRef Val = A->getValue();
+if (Val.equals("MD5")) {
+  CmdArgs.push_back("-gsrc-hash-algorithm=md5");
+} else if (Val.equals("SHA1")) {
+  CmdArgs.push_back("-gsrc-hash-algorithm=sha1");
+} else if (Val.equals("SHA_256")) {
+  CmdArgs.push_back("-gsrc-hash-algorithm=sha256");
+} else {
+  D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
+}
+  }
 }
 
 const char *Clang::getBaseInputName(const ArgList ,
Index: clang/lib/CodeGen/CGDebugInfo.h
===
--- clang/lib/CodeGen/CGDebugInfo.h
+++ clang/lib/CodeGen/CGDebugInfo.h
@@ -572,8 +572,9 @@
   void CreateCompileUnit();
 
   /// Compute the file checksum debug info for input file ID.
-  Optional
-  computeChecksum(FileID FID, SmallString<32> ) const;
+  /// Storage of the checksum string is owned by the caller.
+  Optional>
+  computeChecksum(FileID FID, SmallString<64> ) const;
 
   /// Get the source of the given file ID.
   Optional getSource(const SourceManager , FileID FID);
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -44,9 +44,13 @@
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/SHA1.h"
+#include "llvm/Support/SHA256.h"
 #include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/raw_ostream.h"
 using namespace clang;
 using namespace clang::CodeGen;
 
@@ -361,10 +365,12 @@
   return StringRef();
 }
 
-Optional
-CGDebugInfo::computeChecksum(FileID FID, SmallString<32> ) const {
+Optional>
+CGDebugInfo::computeChecksum(FileID FID, SmallString<64> ) const {
   Checksum.clear();
 
+  llvm::DIFile::ChecksumKind CSKind;
+  Optional> CSInfo;
   if (!CGM.getCodeGenOpts().EmitCodeView &&
   CGM.getCodeGenOpts().DwarfVersion < 5)
 return None;
@@ -374,14 +380,42 @@
   if (!MemBuffer)
 return None;
 
-  llvm::MD5 Hash;
-  llvm::MD5::MD5Result Result;
-
-  Hash.update(MemBuffer->getBuffer());
-  Hash.final(Result);
+  switch (CGM.getCodeGenOpts().getDebugSrcHashAlgorithm()) {
+  case clang::CodeGenOptions::CSK_MD5: {
+CSKind = llvm::DIFile::CSK_MD5;
+llvm::MD5 Hash;
+llvm::MD5::MD5Result Result;
+Hash.update(MemBuffer->getBuffer());
+Hash.final(Result);
+Checksum = Result.digest();
+break;
+  }
+  case clang::CodeGenOptions::CSK_SHA1: {
+CSKind = llvm::DIFile::CSK_SHA1;
+llvm::SHA1 Hash;
+Hash.update(MemBuffer->getBuffer());
+StringRef Result = Hash.final();
+llvm::raw_svector_ostream Res(Checksum);
+for (int i = 0; i < 20; ++i)
+  Res << llvm::format("%.2x", static_cast(Result[i]));
+break;
+  }
+  case