ayzhao updated this revision to Diff 420350.
ayzhao added a comment.
Herald added a subscriber: dexonsmith.

Extract logic into a separate function


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D122766

Files:
  clang/include/clang/Basic/LangOptions.h
  clang/include/clang/Lex/Preprocessor.h
  clang/lib/AST/Expr.cpp
  clang/lib/Basic/LangOptions.cpp
  clang/lib/Lex/PPMacroExpansion.cpp
  clang/test/Preprocessor/file_test_windows.c

STAMPS
actor(@ayzhao) application(Differential) author(@ayzhao) herald(H208) 
herald(H223) herald(H225) herald(H254) herald(H255) herald(H259) herald(H263) 
herald(H277) herald(H278) herald(H288) herald(H310) herald(H311) herald(H313) 
herald(H322) herald(H337) herald(H349) herald(H351) herald(H352) herald(H354) 
herald(H356) herald(H371) herald(H376) herald(H423) herald(H465) herald(H480) 
herald(H515) herald(H519) herald(H524) herald(H576) herald(H615) herald(H688) 
herald(H696) herald(H849) herald(H856) herald(H864) monogram(D122766) 
object-type(DREV) phid(PHID-DREV-5h6hjc7p7oodu7braqun) reviewer(@aaron.ballman) 
reviewer(@hans) reviewer(@mstorsjo) reviewer(@thakis) revision-repository(rG) 
revision-status(needs-review) subscriber(@apazos) subscriber(@arichardson) 
subscriber(@asb) subscriber(@brucehoult) subscriber(@cfe-commits) 
subscriber(@dexonsmith) subscriber(@edward-jones) subscriber(@frasercrmck) 
subscriber(@hiraditya) subscriber(@Jim) subscriber(@jocewei) 
subscriber(@johnrusso) subscriber(@jrtc27) subscriber(@llvm-commits) 
subscriber(@luismarques) subscriber(@MartinMosbeck) subscriber(@MaskRay) 
subscriber(@niosHD) subscriber(@pcwang-thead) subscriber(@PkmX) 
subscriber(@rbar) subscriber(@rnk) subscriber(@rogfer01) subscriber(@s.egerton) 
subscriber(@sabuasal) subscriber(@sameer.abuasal) subscriber(@simoncook) 
subscriber(@the_o) subscriber(@zzheng) tag(#all) tag(#clang) tag(#llvm) 
via(conduit)

Index: clang/test/Preprocessor/file_test_windows.c
===================================================================
--- clang/test/Preprocessor/file_test_windows.c
+++ clang/test/Preprocessor/file_test_windows.c
@@ -8,19 +8,19 @@
 filename: __FILE__
 #include "Inputs/include-file-test/file_test.h"
 
-// CHECK: filename: "A:\\UNLIKELY_PATH\\empty\\file_test_windows.c"
-// CHECK: filename: "A:\\UNLIKELY_PATH\\empty/Inputs/include-file-test/file_test.h"
-// CHECK: basefile: "A:\\UNLIKELY_PATH\\empty\\file_test_windows.c"
+// CHECK: filename: "A:/UNLIKELY_PATH/empty/file_test_windows.c"
+// CHECK: filename: "A:/UNLIKELY_PATH/empty/Inputs/include-file-test/file_test.h"
+// CHECK: basefile: "A:/UNLIKELY_PATH/empty/file_test_windows.c"
 // CHECK-NOT: filename:
 
-// CHECK-EVIL: filename: "A:\\UNLIKELY_PATH=empty\\file_test_windows.c"
-// CHECK-EVIL: filename: "A:\\UNLIKELY_PATH=empty/Inputs/include-file-test/file_test.h"
-// CHECK-EVIL: basefile: "A:\\UNLIKELY_PATH=empty\\file_test_windows.c"
+// CHECK-EVIL: filename: "A:/UNLIKELY_PATH=empty/file_test_windows.c"
+// CHECK-EVIL: filename: "A:/UNLIKELY_PATH=empty/Inputs/include-file-test/file_test.h"
+// CHECK-EVIL: basefile: "A:/UNLIKELY_PATH=empty/file_test_windows.c"
 // CHECK-EVIL-NOT: filename:
 
-// CHECK-CASE: filename: "A:\\UNLIKELY_PATH_BASE\\file_test_windows.c"
-// CHECK-CASE: filename: "A:\\UNLIKELY_PATH_INC\\include-file-test/file_test.h"
-// CHECK-CASE: basefile: "A:\\UNLIKELY_PATH_BASE\\file_test_windows.c"
+// CHECK-CASE: filename: "A:/UNLIKELY_PATH_BASE/file_test_windows.c"
+// CHECK-CASE: filename: "A:/UNLIKELY_PATH_INC/include-file-test/file_test.h"
+// CHECK-CASE: basefile: "A:/UNLIKELY_PATH_BASE/file_test_windows.c"
 // CHECK-CASE-NOT: filename:
 
 // CHECK-REMOVE: filename: "file_test_windows.c"
Index: clang/lib/Lex/PPMacroExpansion.cpp
===================================================================
--- clang/lib/Lex/PPMacroExpansion.cpp
+++ clang/lib/Lex/PPMacroExpansion.cpp
@@ -1511,7 +1511,7 @@
       } else {
         FN += PLoc.getFilename();
       }
-      getLangOpts().remapPathPrefix(FN);
+      processPathForFileMacro(FN, getLangOpts(), getTargetInfo());
       Lexer::Stringify(FN);
       OS << '"' << FN << '"';
     }
@@ -1886,3 +1886,18 @@
     WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
   MI->setIsUsed(true);
 }
+
+void Preprocessor::processPathForFileMacro(SmallVectorImpl<char> &Path,
+                                           const LangOptions &LangOpts,
+                                           const TargetInfo &TI) {
+  LangOpts.remapPathPrefix(Path);
+  if (TI.getTriple().isOSWindows()) {
+    // The path separator character depends on the environment where Clang
+    // _itself_ is built (backslash for Windows, forward slash for *nix). To
+    // make the output of the __FILE__ macro deterministic, we make the path
+    // use forward slashes when targeting Windows independently of how Clang is
+    // built.
+    llvm::sys::path::make_preferred(Path,
+                                    llvm::sys::path::Style::windows_slash);
+  }
+}
Index: clang/lib/Basic/LangOptions.cpp
===================================================================
--- clang/lib/Basic/LangOptions.cpp
+++ clang/lib/Basic/LangOptions.cpp
@@ -62,7 +62,7 @@
   llvm_unreachable("Unknown OpenCL version");
 }
 
-void LangOptions::remapPathPrefix(SmallString<256> &Path) const {
+void LangOptions::remapPathPrefix(SmallVectorImpl<char> &Path) const {
   for (const auto &Entry : MacroPrefixMap)
     if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
       break;
Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -31,6 +31,7 @@
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Lex/LiteralSupport.h"
+#include "clang/Lex/Preprocessor.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/raw_ostream.h"
@@ -2190,7 +2191,8 @@
   switch (getIdentKind()) {
   case SourceLocExpr::File: {
     SmallString<256> Path(PLoc.getFilename());
-    Ctx.getLangOpts().remapPathPrefix(Path);
+    clang::Preprocessor::processPathForFileMacro(Path, Ctx.getLangOpts(),
+                                                 Ctx.getTargetInfo());
     return MakeStringLiteral(Path);
   }
   case SourceLocExpr::Function: {
@@ -2223,7 +2225,8 @@
       StringRef Name = F->getName();
       if (Name == "_M_file_name") {
         SmallString<256> Path(PLoc.getFilename());
-        Ctx.getLangOpts().remapPathPrefix(Path);
+        clang::Preprocessor::processPathForFileMacro(Path, Ctx.getLangOpts(),
+                                                     Ctx.getTargetInfo());
         Value.getStructField(F->getFieldIndex()) = MakeStringLiteral(Path);
       } else if (Name == "_M_function_name") {
         // Note: this emits the PrettyFunction name -- different than what
Index: clang/include/clang/Lex/Preprocessor.h
===================================================================
--- clang/include/clang/Lex/Preprocessor.h
+++ clang/include/clang/Lex/Preprocessor.h
@@ -2582,6 +2582,10 @@
       emitRestrictExpansionWarning(Identifier);
   }
 
+  static void processPathForFileMacro(SmallVectorImpl<char> &Path,
+                                      const LangOptions &LangOpts,
+                                      const TargetInfo &TI);
+
 private:
   void emitMacroDeprecationWarning(const Token &Identifier) const;
   void emitRestrictExpansionWarning(const Token &Identifier) const;
Index: clang/include/clang/Basic/LangOptions.h
===================================================================
--- clang/include/clang/Basic/LangOptions.h
+++ clang/include/clang/Basic/LangOptions.h
@@ -538,7 +538,7 @@
   bool isSYCL() const { return SYCLIsDevice || SYCLIsHost; }
 
   /// Remap path prefix according to -fmacro-prefix-path option.
-  void remapPathPrefix(SmallString<256> &Path) const;
+  void remapPathPrefix(SmallVectorImpl<char> &Path) const;
 };
 
 /// Floating point control options
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to