void InitHeaderSearch::AddPath(const llvm::Twine &Path,
                               IncludeDirGroup Group, bool isCXXAware,
                               bool isUserSupplied, bool isFramework,
                               bool IgnoreSysRoot) {
  assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
  FileManager &FM = Headers.getFileMgr();

  // Compute the actual path, taking into consideration -isysroot.
  llvm::SmallString<256> MappedPathStr;
  llvm::raw_svector_ostream MappedPath(MappedPathStr);

  // Handle isysroot.
  if (Group == System && !IgnoreSysRoot) {
    // FIXME: Portability.  This should be a sys::Path interface, this doesn't
    // handle things like C:\ right, nor win32 \\network\device\blah.
    if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
      MappedPath << isysroot;
  }

  Path.print(MappedPath);

  // Compute the DirectoryLookup type.
  SrcMgr::CharacteristicKind Type;
  if (Group == Quoted || Group == Angled)
    Type = SrcMgr::C_User;
  else if (isCXXAware)
    Type = SrcMgr::C_System;
  else
    Type = SrcMgr::C_ExternCSystem;


  // If the directory exists, add it.
  if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
    IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
                                                  isFramework));
    return;
  }

  // Check to see if this is an apple-style headermap (which are not allowed to
  // be frameworks).
  if (!isFramework) {
    if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
      if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
        // It is a headermap, add it to the search path.
        IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
        return;
      }
    }
  }

  if (Verbose)
    llvm::errs() << "ignoring nonexistent directory \""
                 << MappedPath.str() << "\"\n";
}
Esempio n. 2
0
/* Based on the one from Support/Unix/PathV2 but with different default rights */
static llvm::error_code create_directory(const llvm::Twine& path)
{
    using namespace llvm;
    SmallString<128> path_storage;
    StringRef p = path.toNullTerminatedStringRef(path_storage);
    if (::mkdir(p.begin(), 0755) == -1) {
        if (errno != errc::file_exists)
            return error_code(errno, system_category());
    }
    return error_code::success();
}
Esempio n. 3
0
llvm::error_code canonicalize(const llvm::Twine &path, llvm::SmallVectorImpl<char> &result) {
    std::string p = path.str();
#ifdef PATH_MAX
    int path_max = PATH_MAX;
#else
    int path_max = pathconf(p.c_str(), _PC_PATH_MAX);
    if (path_max <= 0)
        path_max = 4096;
#endif
    result.resize(path_max);
    realpath(p.c_str(), result.data());
    result.resize(strlen(result.data()));
    return llvm::error_code::success();
}
Esempio n. 4
0
VariableSP FindGlobalVariable(TargetSP target, llvm::Twine name) {
  ConstString fullname(name.str());
  VariableList variable_list;
  const bool append = true;
  if (!target) {
    return nullptr;
  }
  const uint32_t match_count = target->GetImages().FindGlobalVariables(
      fullname, append, 1, variable_list);
  if (match_count == 1) {
    return variable_list.GetVariableAtIndex(0);
  }
  return nullptr;
}
Esempio n. 5
0
llvm::error_code create_directories(const llvm::Twine& path)
{
    using namespace llvm;
    using namespace llvm::sys;
    SmallString<128> path_storage;
    StringRef p = path.toStringRef(path_storage);
    StringRef parent = path::parent_path(p);
    if (!parent.empty()) {
        bool parent_exists;
        if (error_code ec = fs::exists(parent, parent_exists)) return ec;

        if (!parent_exists)
            if (error_code ec = create_directories(parent)) return ec;
    }

    return create_directory(p);
}
Esempio n. 6
0
    bool log(const llvm::Twine &message) {
#ifdef DEBUG_PRINTING
        std::cout << "LOG: " << message.str() << std::endl;
#endif
        return false;
    }
Esempio n. 7
0
 bool error(const llvm::Twine &message,bool warning) {
     curErrCallback(message.str(),curFile,curLine,warning,LineRange);
     return true;
 }
static int DiskFilesOrDirectories(const llvm::Twine &partial_name,
                                  bool only_directories, StringList &matches,
                                  TildeExpressionResolver &Resolver) {
  matches.Clear();

  llvm::SmallString<256> CompletionBuffer;
  llvm::SmallString<256> Storage;
  partial_name.toVector(CompletionBuffer);

  if (CompletionBuffer.size() >= PATH_MAX)
    return matches.GetSize();

  namespace path = llvm::sys::path;

  llvm::StringRef SearchDir;
  llvm::StringRef PartialItem;

  if (CompletionBuffer.startswith("~")) {
    llvm::StringRef Buffer(CompletionBuffer);
    size_t FirstSep =
        Buffer.find_if([](char c) { return path::is_separator(c); });

    llvm::StringRef Username = Buffer.take_front(FirstSep);
    llvm::StringRef Remainder;
    if (FirstSep != llvm::StringRef::npos)
      Remainder = Buffer.drop_front(FirstSep + 1);

    llvm::SmallString<256> Resolved;
    if (!Resolver.ResolveExact(Username, Resolved)) {
      // We couldn't resolve it as a full username.  If there were no slashes
      // then this might be a partial username.   We try to resolve it as such
      // but after that, we're done regardless of any matches.
      if (FirstSep == llvm::StringRef::npos) {
        llvm::StringSet<> MatchSet;
        Resolver.ResolvePartial(Username, MatchSet);
        for (const auto &S : MatchSet) {
          Resolved = S.getKey();
          path::append(Resolved, path::get_separator());
          matches.AppendString(Resolved);
        }
      }
      return matches.GetSize();
    }

    // If there was no trailing slash, then we're done as soon as we resolve
    // the expression to the correct directory.  Otherwise we need to continue
    // looking for matches within that directory.
    if (FirstSep == llvm::StringRef::npos) {
      // Make sure it ends with a separator.
      path::append(CompletionBuffer, path::get_separator());
      matches.AppendString(CompletionBuffer);
      return matches.GetSize();
    }

    // We want to keep the form the user typed, so we special case this to
    // search in the fully resolved directory, but CompletionBuffer keeps the
    // unmodified form that the user typed.
    Storage = Resolved;
    llvm::StringRef RemainderDir = path::parent_path(Remainder);
    if (!RemainderDir.empty()) {
      // Append the remaining path to the resolved directory.
      Storage.append(path::get_separator());
      Storage.append(RemainderDir);
    }
    SearchDir = Storage;
  } else {
    SearchDir = path::parent_path(CompletionBuffer);
  }

  size_t FullPrefixLen = CompletionBuffer.size();

  PartialItem = path::filename(CompletionBuffer);

  // path::filename() will return "." when the passed path ends with a
  // directory separator. We have to filter those out, but only when the
  // "." doesn't come from the completion request itself.
  if (PartialItem == "." && path::is_separator(CompletionBuffer.back()))
    PartialItem = llvm::StringRef();

  if (SearchDir.empty()) {
    llvm::sys::fs::current_path(Storage);
    SearchDir = Storage;
  }
  assert(!PartialItem.contains(path::get_separator()));

  // SearchDir now contains the directory to search in, and Prefix contains the
  // text we want to match against items in that directory.

  FileSystem &fs = FileSystem::Instance();
  std::error_code EC;
  llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
  llvm::vfs::directory_iterator End;
  for (; Iter != End && !EC; Iter.increment(EC)) {
    auto &Entry = *Iter;
    llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());

    if (!Status)
      continue;

    auto Name = path::filename(Entry.path());

    // Omit ".", ".."
    if (Name == "." || Name == ".." || !Name.startswith(PartialItem))
      continue;

    bool is_dir = Status->isDirectory();

    // If it's a symlink, then we treat it as a directory as long as the target
    // is a directory.
    if (Status->isSymlink()) {
      FileSpec symlink_filespec(Entry.path());
      FileSpec resolved_filespec;
      auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
      if (error.Success())
        is_dir = fs.IsDirectory(symlink_filespec);
    }

    if (only_directories && !is_dir)
      continue;

    // Shrink it back down so that it just has the original prefix the user
    // typed and remove the part of the name which is common to the located
    // item and what the user typed.
    CompletionBuffer.resize(FullPrefixLen);
    Name = Name.drop_front(PartialItem.size());
    CompletionBuffer.append(Name);

    if (is_dir) {
      path::append(CompletionBuffer, path::get_separator());
    }

    matches.AppendString(CompletionBuffer);
  }

  return matches.GetSize();
}
Esempio n. 9
0
bool pConfigMgr::read(const llvm::Twine &file, pConfig &c) {

    llvm::SmallString<128> path_storage;
    llvm::StringRef fName = file.toStringRef(path_storage);

    llvm::OwningPtr<llvm::MemoryBuffer> contents;
    if (llvm::MemoryBuffer::getFile(fName, contents)) {
        return false;
    }

    llvm::SourceMgr sm;
    llvm::StringRef input = contents->getBufferStart();
    llvm::yaml::Stream stream(input, sm);
    llvm::SmallString<128> kstorage;
    llvm::SmallString<128> vstorage;

    for (llvm::yaml::document_iterator di = stream.begin(), de = stream.end();
       di != de; ++di) {
        llvm::yaml::MappingNode *n = llvm::dyn_cast<llvm::yaml::MappingNode>(di->getRoot());
        if (!n)
            break;
        for (llvm::yaml::MappingNode::iterator i = n->begin();
             i != n->end();
             ++i) {
            llvm::yaml::ScalarNode *keyN = llvm::dyn_cast<llvm::yaml::ScalarNode>(i->getKey());
            llvm::yaml::ScalarNode *valN = llvm::dyn_cast<llvm::yaml::ScalarNode>(i->getValue());
            if (!keyN || !valN)
                continue;
            //std::cout << "key is " << key->getValue(storage).str() << ", val is " << val->getValue(storage).str() << std::endl;

            pStringRef key = keyN->getValue(kstorage);
            pStringRef val = valN->getValue(vstorage);
            if (key == "include") {
                c.includePaths.push_back(val.str());
            }
            else if (key == "source_directory" || key == "source_file") {
                c.inputFiles.push_back(val.str());
            }
            else if (key == "diagnostic_file") {
                c.diagFiles.push_back(val.str());
            }
            else if (key == "db") {
                c.dbName = val.str();
            }
            else if (key == "exts") {
                c.exts = val.str();
            }
            else if (key == "verbosity") {
                llvm::APInt result;
                val.getAsInteger(10, result);
                c.verbosity = result.getLimitedValue();
            }
            else {
                std::cerr << "unknown key in config file: " << key.str() << std::endl;
            }

        }
    }

    return true;

}
Esempio n. 10
0
void AssemblyLogger::log_comment(const llvm::Twine& comment, size_t offset) {
    comments[offset].push_back(comment.str());
}