yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
b118451e3
master
1// slang-artifact-diagnostic-util.cpp 2#include "slang-artifact-diagnostic-util.h" 3 4#include "../core/slang-char-util.h" 5#include "../core/slang-string-util.h" 6 7namespace Slang 8{ 9 10/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactDiagnosticsUtil !!!!!!!!!!!!!!!!!!!!!!!!!!! */ 11 12/* static */ UnownedStringSlice ArtifactDiagnosticUtil ::getSeverityText (Severity severity ) 13{ 14switch (severity ) 15 { 16default : 17return UnownedStringSlice ::fromLiteral ("Unknown" ); 18case Severity ::Info : 19return UnownedStringSlice ::fromLiteral ("Info" ); 20case Severity ::Warning : 21return UnownedStringSlice ::fromLiteral ("Warning" ); 22case Severity ::Error : 23return UnownedStringSlice ::fromLiteral ("Error" ); 24 } 25} 26 27/* static */ SlangResult ArtifactDiagnosticUtil ::splitPathLocation ( 28SliceAllocator & allocator , 29const UnownedStringSlice & pathLocation , 30ArtifactDiagnostic & outDiagnostic ) 31{ 32const Index lineStartIndex = pathLocation .lastIndexOf ('(' ); 33if (lineStartIndex >=0 ) 34 { 35outDiagnostic .filePath = allocator .allocate (pathLocation .head (lineStartIndex ).trim ()); 36 37const UnownedStringSlice tail = pathLocation .tail (lineStartIndex + 1 ); 38const Index lineEndIndex = tail .indexOf (')' ); 39 40if (lineEndIndex >=0 ) 41 { 42// Extract the location info 43UnownedStringSlice locationSlice (tail .begin (),tail .begin ()+ lineEndIndex ); 44 45UnownedStringSlice slices [2 ]; 46const Index numSlices = StringUtil ::split (locationSlice ,',' ,2 ,slices ); 47 48// NOTE! FXC actually outputs a range of columns in the form of START-END in the column 49// position We don't need to parse here, because we only care about the line number 50 51Int lineNumber = 0 ; 52if (numSlices > 0 ) 53 { 54SLANG_RETURN_ON_FAIL (StringUtil ::parseInt (slices [0 ],lineNumber )); 55 } 56 57// Store the line 58outDiagnostic .location .line = lineNumber ; 59 } 60 } 61else 62 { 63outDiagnostic .filePath = allocator .allocate (pathLocation ); 64 } 65return SLANG_OK ; 66} 67 68/* static */ SlangResult ArtifactDiagnosticUtil ::splitColonDelimitedLine ( 69const UnownedStringSlice & line , 70Int pathIndex , 71List < UnownedStringSlice >& outSlices ) 72{ 73StringUtil ::split (line ,':' ,outSlices ); 74 75// Now we want to fix up a path as might have drive letter, and therefore : 76// If this is the situation then we need to have a slice after the one at the index 77if (outSlices .getCount ()> pathIndex + 1 ) 78 { 79const UnownedStringSlice pathStart = outSlices [pathIndex ].trim (); 80if (pathStart .getLength ()== 1 && CharUtil ::isAlpha (pathStart [0 ])) 81 { 82// Splice back together 83outSlices [pathIndex ]= 84UnownedStringSlice (outSlices [pathIndex ].begin (),outSlices [pathIndex + 1 ].end ()); 85outSlices .removeAt (pathIndex + 1 ); 86 } 87 } 88 89return SLANG_OK ; 90} 91 92/* static */ SlangResult ArtifactDiagnosticUtil ::parseColonDelimitedDiagnostics ( 93SliceAllocator & allocator , 94const UnownedStringSlice & inText , 95Int pathIndex , 96LineParser lineParser , 97IArtifactDiagnostics * diagnostics ) 98{ 99List < UnownedStringSlice > splitLine ; 100 101UnownedStringSlice text (inText ),line ; 102while (StringUtil ::extractLine (text ,line )) 103 { 104SLANG_RETURN_ON_FAIL (splitColonDelimitedLine (line ,pathIndex ,splitLine )); 105 106ArtifactDiagnostic diagnostic ; 107diagnostic .severity = Severity ::Error ; 108diagnostic .stage = ArtifactDiagnostic ::Stage ::Compile ; 109diagnostic .location .line = 0 ; 110diagnostic .location .column = 0 ; 111 112if (SLANG_SUCCEEDED (lineParser (allocator ,line ,splitLine ,diagnostic ))) 113 { 114diagnostics -> add (diagnostic ); 115 } 116else 117 { 118// If couldn't parse, just add as a note 119maybeAddNote (line ,diagnostics ); 120 } 121 } 122 123return SLANG_OK ; 124} 125 126/* static */ void ArtifactDiagnosticUtil ::maybeAddNote ( 127const UnownedStringSlice & in , 128IArtifactDiagnostics * diagnostics ) 129{ 130// Don't bother adding an empty line 131if (in .trim ().getLength ()== 0 ) 132 { 133return ; 134 } 135 136// If there's nothing previous, we'll ignore too, as note should be in addition to 137// a pre-existing error/warning 138if (diagnostics -> getCount ()== 0 ) 139 { 140return ; 141 } 142 143// Make it a note on the output 144ArtifactDiagnostic diagnostic ; 145 146String text (in ); 147 148diagnostic .severity = ArtifactDiagnostic ::Severity ::Info ; 149diagnostic .text = SliceUtil ::asTerminatedCharSlice (text ); 150diagnostics -> add (diagnostic ); 151} 152 153 154}// namespace Slang