[PATCH] D91296: Frontend: Clarify logic for using the preamble in ASTUnit::CodeComplete, almost NFC

2020-12-08 Thread Duncan P. N. Exon Smith via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG51f3432f4b52: Frontend: Clarify logic for using the preamble 
in ASTUnit::CodeComplete, almost… (authored by dexonsmith).
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D91296

Files:
  clang/lib/Frontend/ASTUnit.cpp


Index: clang/lib/Frontend/ASTUnit.cpp
===
--- clang/lib/Frontend/ASTUnit.cpp
+++ clang/lib/Frontend/ASTUnit.cpp
@@ -2237,28 +2237,30 @@
 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
   Clang->setCodeCompletionConsumer(AugmentedConsumer);
 
+  auto getUniqueID =
+  [](StringRef Filename) -> Optional {
+if (auto Status = FileMgr.getVirtualFileSystem().status(Filename))
+  return Status->getUniqueID();
+return None;
+  };
+
+  auto hasSameUniqueID = [getUniqueID](StringRef LHS, StringRef RHS) {
+if (LHS == RHS)
+  return true;
+if (auto LHSID = getUniqueID(LHS))
+  if (auto RHSID = getUniqueID(RHS))
+return *LHSID == *RHSID;
+return false;
+  };
+
   // If we have a precompiled preamble, try to use it. We only allow
   // the use of the precompiled preamble if we're if the completion
   // point is within the main file, after the end of the precompiled
   // preamble.
   std::unique_ptr OverrideMainBuffer;
-  if (Preamble) {
-std::string CompleteFilePath(File);
-
-auto  = FileMgr.getVirtualFileSystem();
-auto CompleteFileStatus = VFS.status(CompleteFilePath);
-if (CompleteFileStatus) {
-  llvm::sys::fs::UniqueID CompleteFileID = 
CompleteFileStatus->getUniqueID();
-
-  std::string MainPath(OriginalSourceFile);
-  auto MainStatus = VFS.status(MainPath);
-  if (MainStatus) {
-llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
-if (CompleteFileID == MainID && Line > 1)
-  OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
-  PCHContainerOps, Inv, , false, Line - 1);
-  }
-}
+  if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
+OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
+PCHContainerOps, Inv, (), false, Line - 
1);
   }
 
   // If the main file has been overridden due to the use of a preamble,


Index: clang/lib/Frontend/ASTUnit.cpp
===
--- clang/lib/Frontend/ASTUnit.cpp
+++ clang/lib/Frontend/ASTUnit.cpp
@@ -2237,28 +2237,30 @@
 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
   Clang->setCodeCompletionConsumer(AugmentedConsumer);
 
+  auto getUniqueID =
+  [](StringRef Filename) -> Optional {
+if (auto Status = FileMgr.getVirtualFileSystem().status(Filename))
+  return Status->getUniqueID();
+return None;
+  };
+
+  auto hasSameUniqueID = [getUniqueID](StringRef LHS, StringRef RHS) {
+if (LHS == RHS)
+  return true;
+if (auto LHSID = getUniqueID(LHS))
+  if (auto RHSID = getUniqueID(RHS))
+return *LHSID == *RHSID;
+return false;
+  };
+
   // If we have a precompiled preamble, try to use it. We only allow
   // the use of the precompiled preamble if we're if the completion
   // point is within the main file, after the end of the precompiled
   // preamble.
   std::unique_ptr OverrideMainBuffer;
-  if (Preamble) {
-std::string CompleteFilePath(File);
-
-auto  = FileMgr.getVirtualFileSystem();
-auto CompleteFileStatus = VFS.status(CompleteFilePath);
-if (CompleteFileStatus) {
-  llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID();
-
-  std::string MainPath(OriginalSourceFile);
-  auto MainStatus = VFS.status(MainPath);
-  if (MainStatus) {
-llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
-if (CompleteFileID == MainID && Line > 1)
-  OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
-  PCHContainerOps, Inv, , false, Line - 1);
-  }
-}
+  if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
+OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
+PCHContainerOps, Inv, (), false, Line - 1);
   }
 
   // If the main file has been overridden due to the use of a preamble,
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D91296: Frontend: Clarify logic for using the preamble in ASTUnit::CodeComplete, almost NFC

2020-12-01 Thread Jan Svoboda via Phabricator via cfe-commits
jansvoboda11 accepted this revision.
jansvoboda11 added a comment.
This revision is now accepted and ready to land.

LGTM


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

https://reviews.llvm.org/D91296

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


[PATCH] D91296: Frontend: Clarify logic for using the preamble in ASTUnit::CodeComplete, almost NFC

2020-11-18 Thread Duncan P. N. Exon Smith via Phabricator via cfe-commits
dexonsmith added a comment.

ping


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

https://reviews.llvm.org/D91296

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


[PATCH] D91296: Frontend: Clarify logic for using the preamble in ASTUnit::CodeComplete, almost NFC

2020-11-11 Thread Duncan P. N. Exon Smith via Phabricator via cfe-commits
dexonsmith created this revision.
dexonsmith added reviewers: arphaman, JDevlieghere, jansvoboda11.
Herald added a subscriber: ributzka.
dexonsmith requested review of this revision.

Clarify the logic for using the preamble (and overriding the main file
buffer) in `ASTUnit::CodeComplete` by factoring out a couple of lambdas
(`getUniqueID` and `hasSameUniqueID`).  While refactoring the logic,
hoist the check for `Line > 1` and locally check if the filenames are
equal (both to avoid unnecessary `stat` calls) and skip copying out the
filenames to `std::string`.

Besides fewer calls to `stat`, there's no functionality change here.


https://reviews.llvm.org/D91296

Files:
  clang/lib/Frontend/ASTUnit.cpp


Index: clang/lib/Frontend/ASTUnit.cpp
===
--- clang/lib/Frontend/ASTUnit.cpp
+++ clang/lib/Frontend/ASTUnit.cpp
@@ -2229,28 +2229,30 @@
 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
   Clang->setCodeCompletionConsumer(AugmentedConsumer);
 
+  auto getUniqueID =
+  [](StringRef Filename) -> Optional {
+if (auto Status = FileMgr.getVirtualFileSystem().status(Filename))
+  return Status->getUniqueID();
+return None;
+  };
+
+  auto hasSameUniqueID = [getUniqueID](StringRef LHS, StringRef RHS) {
+if (LHS == RHS)
+  return true;
+if (auto LHSID = getUniqueID(LHS))
+  if (auto RHSID = getUniqueID(RHS))
+return *LHSID == *RHSID;
+return false;
+  };
+
   // If we have a precompiled preamble, try to use it. We only allow
   // the use of the precompiled preamble if we're if the completion
   // point is within the main file, after the end of the precompiled
   // preamble.
   std::unique_ptr OverrideMainBuffer;
-  if (Preamble) {
-std::string CompleteFilePath(File);
-
-auto  = FileMgr.getVirtualFileSystem();
-auto CompleteFileStatus = VFS.status(CompleteFilePath);
-if (CompleteFileStatus) {
-  llvm::sys::fs::UniqueID CompleteFileID = 
CompleteFileStatus->getUniqueID();
-
-  std::string MainPath(OriginalSourceFile);
-  auto MainStatus = VFS.status(MainPath);
-  if (MainStatus) {
-llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
-if (CompleteFileID == MainID && Line > 1)
-  OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
-  PCHContainerOps, Inv, , false, Line - 1);
-  }
-}
+  if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
+OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
+PCHContainerOps, Inv, (), false, Line - 
1);
   }
 
   // If the main file has been overridden due to the use of a preamble,


Index: clang/lib/Frontend/ASTUnit.cpp
===
--- clang/lib/Frontend/ASTUnit.cpp
+++ clang/lib/Frontend/ASTUnit.cpp
@@ -2229,28 +2229,30 @@
 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
   Clang->setCodeCompletionConsumer(AugmentedConsumer);
 
+  auto getUniqueID =
+  [](StringRef Filename) -> Optional {
+if (auto Status = FileMgr.getVirtualFileSystem().status(Filename))
+  return Status->getUniqueID();
+return None;
+  };
+
+  auto hasSameUniqueID = [getUniqueID](StringRef LHS, StringRef RHS) {
+if (LHS == RHS)
+  return true;
+if (auto LHSID = getUniqueID(LHS))
+  if (auto RHSID = getUniqueID(RHS))
+return *LHSID == *RHSID;
+return false;
+  };
+
   // If we have a precompiled preamble, try to use it. We only allow
   // the use of the precompiled preamble if we're if the completion
   // point is within the main file, after the end of the precompiled
   // preamble.
   std::unique_ptr OverrideMainBuffer;
-  if (Preamble) {
-std::string CompleteFilePath(File);
-
-auto  = FileMgr.getVirtualFileSystem();
-auto CompleteFileStatus = VFS.status(CompleteFilePath);
-if (CompleteFileStatus) {
-  llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID();
-
-  std::string MainPath(OriginalSourceFile);
-  auto MainStatus = VFS.status(MainPath);
-  if (MainStatus) {
-llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
-if (CompleteFileID == MainID && Line > 1)
-  OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
-  PCHContainerOps, Inv, , false, Line - 1);
-  }
-}
+  if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
+OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
+PCHContainerOps, Inv, (), false, Line - 1);
   }
 
   // If the main file has been overridden due to the use of a preamble,
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits