Skip to content
Permalink
Browse files

Properly escape filenames in Windows

  • Loading branch information...
facchinm committed Oct 9, 2017
1 parent 1639a2a commit 8217efdb2abbf75d36fd9e3f410a9d06c0001d14
Showing with 29 additions and 3 deletions.
  1. +7 −2 JsonImpl.hpp
  2. +3 −1 main.cpp
  3. +19 −0 utils.hpp
@@ -38,6 +38,7 @@ using namespace std;

#define JSON_NOEXCEPTION
#include "json.hpp"
#include "utils.hpp"
#include "clang/include/clang/Sema/CodeCompleteConsumer.h"
using json = nlohmann::json;

@@ -50,8 +51,10 @@ inline json encode(const SourceManager &sm, const SourceLocation &loc) {
PresumedLoc presumed = sm.getPresumedLoc(loc);
stringstream pos;
pos << presumed.getLine() << ":" << presumed.getColumn();
std::string filename(presumed.getFilename());
filename = quoteCppString(filename);
return json{
{"file", presumed.getFilename()},
{"file", filename.c_str()},
{"pos", pos.str()}};
}

@@ -225,7 +228,9 @@ inline json encode(const CodeCompletionResult &cc, const CodeCompletionString *c
{
SourceLocation loc = cc.Declaration->getLocation();
PresumedLoc presumedLoc = sm.getPresumedLoc(loc);
res["location"] = presumedLoc.getFilename();
std::string filename(presumedLoc.getFilename());
filename = quoteCppString(filename);
res["location"] = filename.c_str();
res["type"] = cc.Declaration->getDeclKindName();

// For each parameter extract type and name
@@ -233,7 +233,9 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
ostringstream lineInfo;
lineInfo << "#line " << presumed.getLine();
lineInfo << " \"" << presumed.getFilename() << "\"\n";
rewriter.InsertTextAfter(insertionPoint, lineInfo.str());
std::string lineInfoAsStr = lineInfo.str();
lineInfoAsStr = quoteCppString(lineInfoAsStr);
rewriter.InsertTextAfter(insertionPoint, lineInfoAsStr);
}
};

@@ -75,3 +75,22 @@ inline bool cStrEndsWith(const char *str, const char *suffix) {

return strncmp(str + strLen - suffixLen, suffix, suffixLen) == 0;
}

#ifdef WIN32
inline bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}

inline std::string quoteCppString(std::string& str) {
replace(str, "\\", "\\\\");
return str;
}
#else
inline std::string quoteCppString(std::string& str) {
return str;
}
#endif

0 comments on commit 8217efd

Please sign in to comment.
You can’t perform that action at this time.