Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/Distro.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Distro.cpp - Linux distribution detection support ------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "clang/Driver/Distro.h"
10
#include "clang/Basic/LLVM.h"
11
#include "llvm/ADT/SmallVector.h"
12
#include "llvm/ADT/StringRef.h"
13
#include "llvm/ADT/StringSwitch.h"
14
#include "llvm/Support/ErrorOr.h"
15
#include "llvm/Support/MemoryBuffer.h"
16
17
using namespace clang::driver;
18
using namespace clang;
19
20
72.0k
static Distro::DistroType DetectDistro(llvm::vfs::FileSystem &VFS) {
21
72.0k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
22
72.0k
      VFS.getBufferForFile("/etc/lsb-release");
23
72.0k
  if (File) {
24
2
    StringRef Data = File.get()->getBuffer();
25
2
    SmallVector<StringRef, 16> Lines;
26
2
    Data.split(Lines, "\n");
27
2
    Distro::DistroType Version = Distro::UnknownDistro;
28
2
    for (StringRef Line : Lines)
29
10
      if (Version == Distro::UnknownDistro && 
Line.startswith("DISTRIB_CODENAME=")6
)
30
2
        Version = llvm::StringSwitch<Distro::DistroType>(Line.substr(17))
31
2
                      .Case("hardy", Distro::UbuntuHardy)
32
2
                      .Case("intrepid", Distro::UbuntuIntrepid)
33
2
                      .Case("jaunty", Distro::UbuntuJaunty)
34
2
                      .Case("karmic", Distro::UbuntuKarmic)
35
2
                      .Case("lucid", Distro::UbuntuLucid)
36
2
                      .Case("maverick", Distro::UbuntuMaverick)
37
2
                      .Case("natty", Distro::UbuntuNatty)
38
2
                      .Case("oneiric", Distro::UbuntuOneiric)
39
2
                      .Case("precise", Distro::UbuntuPrecise)
40
2
                      .Case("quantal", Distro::UbuntuQuantal)
41
2
                      .Case("raring", Distro::UbuntuRaring)
42
2
                      .Case("saucy", Distro::UbuntuSaucy)
43
2
                      .Case("trusty", Distro::UbuntuTrusty)
44
2
                      .Case("utopic", Distro::UbuntuUtopic)
45
2
                      .Case("vivid", Distro::UbuntuVivid)
46
2
                      .Case("wily", Distro::UbuntuWily)
47
2
                      .Case("xenial", Distro::UbuntuXenial)
48
2
                      .Case("yakkety", Distro::UbuntuYakkety)
49
2
                      .Case("zesty", Distro::UbuntuZesty)
50
2
                      .Case("artful", Distro::UbuntuArtful)
51
2
                      .Case("bionic", Distro::UbuntuBionic)
52
2
                      .Case("cosmic", Distro::UbuntuCosmic)
53
2
                      .Case("disco", Distro::UbuntuDisco)
54
2
                      .Case("eoan", Distro::UbuntuEoan)
55
2
                      .Default(Distro::UnknownDistro);
56
2
    if (Version != Distro::UnknownDistro)
57
2
      return Version;
58
72.0k
  }
59
72.0k
60
72.0k
  File = VFS.getBufferForFile("/etc/redhat-release");
61
72.0k
  if (File) {
62
2
    StringRef Data = File.get()->getBuffer();
63
2
    if (Data.startswith("Fedora release"))
64
1
      return Distro::Fedora;
65
1
    if (Data.startswith("Red Hat Enterprise Linux") ||
66
1
        Data.startswith("CentOS") ||
67
1
        
Data.startswith("Scientific Linux")0
) {
68
1
      if (Data.find("release 7") != StringRef::npos)
69
1
        return Distro::RHEL7;
70
0
      else if (Data.find("release 6") != StringRef::npos)
71
0
        return Distro::RHEL6;
72
0
      else if (Data.find("release 5") != StringRef::npos)
73
0
        return Distro::RHEL5;
74
0
    }
75
0
    return Distro::UnknownDistro;
76
0
  }
77
72.0k
78
72.0k
  File = VFS.getBufferForFile("/etc/debian_version");
79
72.0k
  if (File) {
80
2
    StringRef Data = File.get()->getBuffer();
81
2
    // Contents: < major.minor > or < codename/sid >
82
2
    int MajorVersion;
83
2
    if (!Data.split('.').first.getAsInteger(10, MajorVersion)) {
84
1
      switch (MajorVersion) {
85
1
      case 5:
86
0
        return Distro::DebianLenny;
87
1
      case 6:
88
0
        return Distro::DebianSqueeze;
89
1
      case 7:
90
0
        return Distro::DebianWheezy;
91
1
      case 8:
92
1
        return Distro::DebianJessie;
93
1
      case 9:
94
0
        return Distro::DebianStretch;
95
1
      case 10:
96
0
        return Distro::DebianBuster;
97
1
      case 11:
98
0
        return Distro::DebianBullseye;
99
1
      default:
100
0
        return Distro::UnknownDistro;
101
1
      }
102
1
    }
103
1
    return llvm::StringSwitch<Distro::DistroType>(Data.split("\n").first)
104
1
        .Case("squeeze/sid", Distro::DebianSqueeze)
105
1
        .Case("wheezy/sid", Distro::DebianWheezy)
106
1
        .Case("jessie/sid", Distro::DebianJessie)
107
1
        .Case("stretch/sid", Distro::DebianStretch)
108
1
        .Case("buster/sid", Distro::DebianBuster)
109
1
        .Case("bullseye/sid", Distro::DebianBullseye)
110
1
        .Default(Distro::UnknownDistro);
111
1
  }
112
72.0k
113
72.0k
  File = VFS.getBufferForFile("/etc/SuSE-release");
114
72.0k
  if (File) {
115
3
    StringRef Data = File.get()->getBuffer();
116
3
    SmallVector<StringRef, 8> Lines;
117
3
    Data.split(Lines, "\n");
118
6
    for (const StringRef& Line : Lines) {
119
6
      if (!Line.trim().startswith("VERSION"))
120
3
        continue;
121
3
      std::pair<StringRef, StringRef> SplitLine = Line.split('=');
122
3
      // Old versions have split VERSION and PATCHLEVEL
123
3
      // Newer versions use VERSION = x.y
124
3
      std::pair<StringRef, StringRef> SplitVer = SplitLine.second.trim().split('.');
125
3
      int Version;
126
3
127
3
      // OpenSUSE/SLES 10 and older are not supported and not compatible
128
3
      // with our rules, so just treat them as Distro::UnknownDistro.
129
3
      if (!SplitVer.first.getAsInteger(10, Version) && Version > 10)
130
2
        return Distro::OpenSUSE;
131
1
      return Distro::UnknownDistro;
132
1
    }
133
3
    
return Distro::UnknownDistro0
;
134
72.0k
  }
135
72.0k
136
72.0k
  if (VFS.exists("/etc/exherbo-release"))
137
1
    return Distro::Exherbo;
138
72.0k
139
72.0k
  if (VFS.exists("/etc/alpine-release"))
140
0
    return Distro::AlpineLinux;
141
72.0k
142
72.0k
  if (VFS.exists("/etc/arch-release"))
143
1
    return Distro::ArchLinux;
144
72.0k
145
72.0k
  if (VFS.exists("/etc/gentoo-release"))
146
1
    return Distro::Gentoo;
147
72.0k
148
72.0k
  return Distro::UnknownDistro;
149
72.0k
}
150
151
72.0k
Distro::Distro(llvm::vfs::FileSystem &VFS) : DistroVal(DetectDistro(VFS)) {}